ASP.NET - Problem with plain/text view - Asked By mostafa hamdy on 30-Nov-11 05:25 AM

Hello all
I have some web application which save files in DB as a binary and I use the following code:

string contentType = FileUpload1.PostedFile.ContentType;

// Get the bytes from the uploaded file

byte[] fileData = new byte[FileUpload1.PostedFile.InputStream.Length];

FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length);


// Get the name without folder information from the uploaded file.

string originalName = Path.GetFileName(FileUpload1.PostedFile.FileName);

// Create a new instance of the File class based on the uploaded file.

File myFile = new File(contentType, originalName, fileData);

// Save the file, and tell the Save method what data store to use.

switch (AppConfiguration.DataStoreType)

{


case DataStoreType.Database:

myFile.Save();


break;

case DataStoreType.FileSystem:

myFile.Save(Server.MapPath(Path.Combine(AppConfiguration.UploadsFolder, myFile.FileUrl)));

break;

}


the problem is when I try to get the file from DB of content type =plain/text to view them the IE and the Firefox browsers couldn't  disply them and I get the folowing erroe in the IE

"Invalid at the top level of the document. Error processing resource"
the code which I used to view is like the following:

Response.Clear();


Guid id = new Guid(Request.QueryString.Get("Id"));

File myFile = File.GetItem(id);

Response.ContentType = myFile.ContentType;


if (myFile.ContainsFile)

{


if(Response.ContentType=="text/plain")

{

Response.Charset =

"windows-1250";

Response.BinaryWrite(myFile.FileData);

Response.End();

}


else

{

Response.BinaryWrite(myFile.FileData);

}


}

please if any body get what I mean and can help me please send me or tell me about some URL may help me in solving this issue
regards
Mostafa
Reena Jain replied to mostafa hamdy on 30-Nov-11 05:32 AM
Hi,

this inline script page works fine for me:
<%@ Page Language="C#" %>
<script runat=server>
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
Response.Write("help");
}
</script>

Then check SamplePage.aspx, make sure there are no extra characters at the begining.
 
http://www.bing.com/search?q=asp.net+%22Error%3A+Invalid+at+the+top+level+of+the+document%22&form=QBRE&qs=n[http://www.bing.com/search?q=asp.net+%22Error%3A+Invalid+at+the+top+level+of+the+document%22&form=QBRE&qs=n]
mostafa hamdy replied to Reena Jain on 30-Nov-11 05:40 AM
Hi Reena
thanx about ur help, but the problem stil exist with  mw , any other recommendations
regards
Mostafa