Outputting .RTF file to response Corrupt - c#

I have an AJAX file uploader on my page, and use this to store various different file types into a database, However when I store a .rtf file and then try to output this using response, I get a file corrupt and cannot be opened error message. The file uploader automatically detects the mime type of the file on upload and stores the file as "application/msword" as this didn't work I tried giving it several other mime types however none of them worked.
Does anyone know of any reasons why this file may be corrupt?
PS. I have also tried uploading several different .rtf files with text, no text and images, etc. however none of them worked. output code below:
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = displayFile.MimeType;
context.Response.AddHeader("content-disposition", "attachment; filename=" + displayFile.FileName);
context.Response.BinaryWrite(displayFile.Data);
context.Response.End();

Related

Error trying to send zip file to navigator

I'm trying to send a zip file to navigator in ASP NET Visual Basic, I do the following:
Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.ContentType = "application/zip"
Response.WriteFile(fullPath)
Response.End()
I get this strange characters
How can I send the zip to navigator?
A zip file is NOT a text file. Don't treat it like a text file. Don't try viewing it like a text file.
Rather, a ZIP file is a binary file format. It is a container (also often called an archive) that contains compressed files. What you see there in your screenshot is just the binary data of the ZIP file. Binary data which whatever viewer you used tried to interpret as character codes -- which of course will not yield any sensible result, because a ZIP file is not a text file. It's not.
If you want to get your hands on the files compressed within the ZIP file, you need to uncompress ("unpack") the ZIP file.
By the way, i can see that the compressed file within the ZIP file is presumably a PDF file. Which is also not a text file, but requires a PDF reader/viewer to display/render it (after it has been unpacked from the ZIP file, obviously...).

Amazon S3 - MS office files corrupted when downloading

When I upload a docx, xlsx or pptx to amazon S3 using aws .net sdk the file is getting uploaded fine and I am able to view the file directly from S3 without any issues. But when I download the file in ASP.net using C# I am getting a warning message (see below) when opening the file:
"Excel found unreadable content in test.xlsx. Do you want to recover the contents of this workbook?" If I click yes, I am able to see all the contents in the document.
similarly I am getting a warning message for .docx file as well.
This is the C# code I am using:
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.OutputStream.Write(Content, 0, Content.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Update: When I download the file in windows forms app it is working fine and the problem is with web app only.
What am I doing wrong?
Please help...
More information is needed to figure this out. The code snippet you provided looks OK. The problem looks to be elsewhere in your code... For example, what are you loading inside "Content"?
Also, it would help if you tried uploading and downloading a simple text file, to see what kind of data gets appended.
If Content is a byte[] created from a MemoryStream, make sure you're calling .ToArray() and not .GetBuffer() to get it. If you use .GetBuffer(), you'll potentially get extra bytes on the end, which the office file format probably doesn't like. Your other file types you've tried it with might be more tolerant of extra data they weren't expecting on the end.

Response.TransmitFile corrupting file

I have a file on my server or well in this localhost. I want to transmit it to user using this code:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.pdf");
Response.TransmitFile(Server.MapPath("~/buylist.pdf"));
Response.ContentType = "application/csv";
Response.AppendHeader("Content-Disposition", "attachment; filename=buylist.csv");
Response.TransmitFile(Server.MapPath("~/buylist.csv"));
I know the file I am trying to transmit our fine and I even tried adding Response.Close() but each time the file is corrupted. The csv give me the page's HTML. I am really lost.
Try putting a Response.Clear() at the top of this code, and a Response.End() and bottom.
Also, it looks like you're trying to transmit two files in one response, and that just won't work. It might also be two versions of the code showing in the same snippet in your question, but this is still worth mentioning: You need to pick one file to return in one response. If you absolutely must return two files at once, you'll have to zip or tar them together first. There is no way to send two files with the same response.

Loading PDF from BLOB stream (MySQL/C#)

I am using
BLOBs in MySQL
C# VS 2010
PDF Reader Component ( added from C#) to
display the PDF
I have an application that uses MySQL as backend and C# as front. i have added a PDF Reader component to my form and i wish to show the pdf on that form. the pdf can be accessed using the loadFile method of the component. My PDFs are stored as BLOBs in MySQL which i extract and copy it to the disk. I then link the path to the loadFile Argument to display my pdf..
This all works fine but now i would like to know if there is a way so that i can display the pdf (stored as a blob in MySQL) without copying it to the hard disk first.
Have you tried reading it directly to a binary array and then sending it to the output? Something along these lines should work. Your equivalent data adapter would work in the first line.
// Generate Report
byte[] data = (byte[])dataTable.Rows[0]["BLOB"];
// Present the generated PDF to the user
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", data.Length.ToString());
Response.BinaryWrite(data);
Response.Flush();
Response.Close();

Issue opening doc/docx/excel files in browser(Asp.net, C#)

I am storing some files like PDF, Doc, Docx, xls etc. in the file system.
Now I have to show those files on the browser for the users to view.
So basically a link button is there in a datagrid, upon clicking on which the user will be able to view.
So far my code goes like this
Response.Clear();
// Response.ContentType = "application/pdf";
//Response.ContentType = "application/x-msexcel";
//Response.ContentType = "application/msword";
string strFilePath = #"C:\test.pdf";
// string strFilePath = #"C:\test.doc";
Response.WriteFile(strFilePath);
Response.End();
This works fine for pdf files but fails for word or excel files. In the browser I am able to view the pdf but the other files are not opening in browser but are asking for the
Save , Open dialog box option. If I click on Open, it will open in a normal window instead of browser.
How can I achieve my target? The browser is IE
Thanks in advance
have you tried adding something like this?
Response.AppendHeader("Content-Disposition", "inline;filename=" + "ExcelFile.xls");
There may be a browser configuration issue.
Try changing some settings in the browser(Custom Level).
Launching programs and files in an Iframe
Launching applications and unsafe files.
etc

Categories