I have to download file from server to browser. The code actually works, but it appends '_' characters to the beginning and end of the file.
Could you please help me to solve that? I have no idea why that strange stuff happens...
Here's the codes:
Response.AddHeader("Content-Disposition", "attachment; filename=\" + "p.jpg" + "\")
Response.ContentType = "application/octet-stream"
Response.WriteFile(Server.MapPath("~/App_data/PROJECTS_DATA/p.jpg"))
Use Below:
Response.ContentType = "image/jpg";
Related
I have written code to open a Word document after downloading it in code behind. The document is opening fine, but it is not saving in Word format. When I am going to open it, it is asking for selecting the format to open the file.
The code is below:
string FullFilePath = "D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
Set your content type to application/msword.
Refer: Microsoft Office MIME Types
You are not specifying an extension when sending the file name.
If your file saves without an extension, you will get the prompt asking for the application to use to open it.
Also, use the "Content-Disposition", "Attachment" if you want to tell the browser to save the file. inline will make the browser attempt to open the file in Word directly.
string FullFilePath =//path of file //"D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
I try to download a docx file from serverside.
What is my wrong ?
this is code :
FileInfo file = new FileInfo(filepath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "attachment; filename = " + ((Button)sender).CommandName + ".docx");
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();
I have posted something similar in another question for an PDF but here goes. It's much easier to stream this sort of data back through an ASHX handler.
Something like what I posted in this question but with a docx file.
Display PDF in iframe
It looks like you are using a normal ASP.NET page and are trying to modify the standard behavior by clearing out the headers, etc. You won't have to fiddle with the headers or anything like that with an ashx handler.
I am writing a webapplication in ASP.net.
I am trying to make a file dialog box appear for downloading something off the server.
I have the appropriate file data stored in a variable called file.
File has fields:
FileType - The MIMEType of the file
FilePath - The server-side file path
Here's the code so far:
Response.Clear();
Response.ContentType = file.FileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + GetFileName(file));
Response.TransmitFile(file.FilePath) ;
Response.End();
GetFileName is a function that gets me the filename from an attachment object. I only store the path.
The above code is in a function called "Download_Clicked" that is an event that triggers on click. The event is mapped to a LinkButton.
The problem is that when I run the above code, nothing happens. The standard dialog box does not appear.
I have attempted the standard trouble-shooting such as making sure the file exists, and ensuring the path is correct. They are both dead on the mark.
My guess is that because my machine is also the server, it may not be processing properly somehow.
Thanks in advance.
Edit 1: Attempted putting control onto another page, works fine.
Edit 2: Resolved issue by removing control from AJAX Update Panel.
I've found another to do this without removing the update panel. Place the code below in your page load and you'll now be able to use that button to trigger a download.
ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button);
Use Response.WriteFile() instead.
Also, don't use Response.End()! This aborts the thread. Use Response.Flush(); Response.Close();
Try changing
Response.AppendHeader("Content-Disposition", "attachment; filename=" + GetFileName(file));
To
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(GetFileName(file))));
If that doesn't work, you can always use Response.BinaryWrite or Resonse.Write to stream the file to the web browser
Here is how transmit the file using Response.Write or Response.BinaryWrite. Put these functions in a library somewhere then call them as needed
public void SendFileToBrowser(String FileName, String MIMEType, String FileData)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = MIMEType;
Response.Buffer = true;
Response.Write(FileData);
Response.End();
}
public void SendFileToBrowser(String FileName, String MIMEType, Byte[] FileData)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = MIMEType;
Response.Buffer = true;
Response.BinaryWrite(FileData);
Response.End();
}
Then somewhere you call these functions like so
SendFileToBrowser("FileName.txt", "text/plain", "Don't try this from an Update Panel. MSAjax does not like it when you mess with the response stream.");
See edit on initial post.
Removed Ajax Update Panel to resolve the error. The panel was stopping the post back to the server.
For more info, see Cris Valenzuela's comment.
So, I user a bit of code to force a download on my ASP.Net based project. This bit of code works in Firefox and Chrome, bu not in IE for some strange reason. Even stranger, it worked in all three initially, and just stopped working in IE recently. Below is the code I used, please let me know if any adjustments need to be made or what the problem with with may be.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.AppendHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
More details
Here is the revamped code, still doesnt work for IE.
string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "Application/pdf";
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = type;
Response.AddHeader("content-disposition","attachment; filename=" + path);
Response.WriteFile(path);
Response.End();
You should probably try to set the mime type to "application/octet-stream". If you don't want a specific handler to respond to the mime-type.
Should this code
Response.AddHeader("content-disposition","attachment; filename=" + path);
be changed as
Response.AddHeader("content-disposition","attachment; filename=" + name + "." + ext);
or
Response.AddHeader("content-disposition","attachment; filename=" + name + ".pdf");
Other things to check for
Response.Buffer to true in the beginning
Response.clear in the beginning
Use response.binarywrite instead of writefile
Response flush at the end
Ensure no HTML or space characters written to the response.stream other than the binarywrite.
Problem solved. The reason it was not going through was due to an extra form that was present on the master page, apparently overlaying the buttons. Once that was fixed, It worked properly in all browsers.
Adding the following two lines at the top, fixed it for me:
Response.ClearContent();
Response.ClearHeaders();
I'm trying to stream a text file (CSV) to the response, and the following code works perfectly in Firefox 3, but when I use IE, it looks like it wants to download the actual .aspx page, and complains that the file contents don't match the file extension or type. If I then choose to download the file anyway, it correctly downloads the CSV data and opens it in Excel. What am I doing wrong?
DataTable dt = ExtensionsProvider.ListPrivateCallCostsForCsv(reportFilter.BusinessUnit, reportFilter.StartDate,
reportFilter.EndDate);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "filename=" + GetExportFileName());
DataTableHelper.WriteCsv(dt, Response.Output, false);
Response.End();
Response.AddHeader("Content-Disposition", "filename=" + GetExportFileName());
Should be:
Response.AddHeader("Content-Disposition", "attachment;filename=" + GetExportFileName());
Without a main Content-Disposition value, IE will just use the trailing part of the URL — something.aspx — as a filename.
(The above assumes GetExportFileName() returns a sanitised filename stripped of most punctuation. What can go in a header parameter as token or quoted-string in IE is a matter of some annoyance; see this question for details)
It does not work with inline either. Of course it works for all other browsers
HttpServletResponse response = aCtx.getResponse();
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "inline;filename=log.txt");
You have to give the value for the Content-Disposition header, in addition to the filename parameter.
You may have more luck with the "inline" value than the "attachment" value:
Response.AddHeader("Content-Disposition", "inline;filename=" + GetExportFileName());