I have a file, and it is full path (plus the file name) is in this variable:
fileTemporary
i want to download that file to the client.
i do this:
HttpContext.Current.Response.TransmitFile(fileTemporary);
but nothing happen, i mean when i click the button, this file executes, but nothing is being downloaded to the client. i don't see any file on the browser of the client.
what mistake did i do please?
If you use MVC you can:
[HttpGet]
public virtual ActionResult GetFile(string fileTemporary)
{
// ...preparing file path... init fileTemporary.
var bytes = System.IO.File.ReadAllBytes(fileTemporary);
var fileContent = new FileContentResult(bytes, "binary/octet-stream");
Response.AddHeader("Content-Disposition", "attachment; filename=\"YourFileName.txt\"");
return fileContent;
}
If you use ASP.NET or whatever you can use following (sorry, my old code, but you can understand approach):
var bytes = System.IO.File.ReadAllBytes(fileTemporary);
SendFileBytesToResponse(bytes, fileName);
public static bool SendFileBytesToResponse(byte[] bytes, string sFileName)
{
if (bytes!= null)
{
string downloadName = sFileName;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition",
"attachment; filename=" + downloadName + "; size=" + bytes.Length.ToString());
response.Flush();
response.BinaryWrite(bytes);
response.Flush();
response.End();
}
return true;
}
Without reingeneering your solution:
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.AddHeader("Content-Type", "binary/octet-stream");
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName);
System.Web.HttpContext.Current.Response.TransmitFile(fileName);
If you would like browser to interpret you file right, you will need to specify header "Content-Type" more precise.
Please see list of content types
Related
So I have a controller method that creates an excel package and sends the response back to the user to save/download. The problem that I am having is that the response is not prompting the Open/Save dialog from appearing. Is there something I am missing?
[HttpPost]
public ActionResult Export()
{
var excel = do stuff to create open XML package
using (var memoryStream = new MemoryStream())
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + Convert.ToString(fileName).Replace(" ", "_") + ".xlsx");
excel.SaveAs(memoryStream);
memoryStream.WriteTo(Response.OutputStream);
Response.BinaryWrite(excel.GetAsByteArray());
Response.Flush();
Response.End();
}
return null
I am trying to download an excel file which for which I am giving the proper path. But after downloading it when I am trying to open I get error as
excel cannot open the file because the file format or file extension is not valid. Verify that file has been corrupted...
But If I open that file directly it gets open, I don't understand what is the issue in it. Below is my code
protected void MultipleSpanLinkIdDownLoadbtn_Click(object sender, EventArgs e)
{
try
{
string fileName = "multiSpanLinkeid.xlsx";
FileInfo file = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["MultipleSpanLinkFolder"].ToString() + "/" + fileName);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/vnd.xls";
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
catch (Exception)
{
ClientScript.RegisterStartupScript(this.GetType(), "Exception", "alert('Template downloaded.');", true);
}
}
Please suggest what is wrong in this
NOTE: The issue arises while downloading it from Chrome
Please try with this content type.
Response.ContentType = "application/vnd.ms-excel";
or
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Try this:
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
When I download a JPEG file from and ASP Web Forms application using the following code, the image is not shown when the application runs from a browser (Safari) on an iPhone 5 - only a screen offering to open the file in Dropbox. Using Opera the download does not appear at all. It appears that IOS does not recognize the downloaded file as a displayable image. The download occurs as expected from a Windows desktop.
string sDownloadFile = Session["strImagePath"].ToString();
string sFileName = Session["sFileName"].ToString();
Response.ContentType = "application/jpeg";
Response.AppendHeader("Content-Disposition", "attachment; filename='" + sFileName + "'");
Response.TransmitFile(sDownloadFile);
Response.End();
Using the code below (BinaryWrite rather than TransmitFile) I found that the iPhone would recognize downloaded file as a JPEG.
string sFileName = Session["sFileName"].ToString();// Original file name// System.IO.Path.GetFileName(sDownloadFile);
byte[] fileBytes = System.IO.File.ReadAllBytes(sDownloadFile);
SendFileBytesToResponse(fileBytes, sFileName);
private static void SendFileBytesToResponse(byte[] bytes, string sFileName)
{
if (bytes != null)
{
string downloadName = sFileName;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "application/jpeg");
response.AddHeader("Content-Disposition",
"attachment; filename=" + downloadName + "; size=" + bytes.Length.ToString());
response.Flush();
response.BinaryWrite(bytes);
response.Flush();
response.End();
}
}
the below code works if I try to download a .txt/.msg/.pdf/.png/.jpg but it fails if I try to download exccel or word files. Can anybody please help
Response.ContentType = "APPLICATION/OCTET-STREAM";
//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
fileUrl = Server.UrlDecode(fileUrl);
System.String disHeader = "attachment; filename=\"" + fileUrl + "\"";
Response.AppendHeader("Content-Disposition", disHeader);
// transfer the file byte-by-byte to the response objectÂ
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(fileUrl);
Response.Flush();
Response.WriteFile(fileToDownload.FullName);
Here is a piece of my code:
if (objTbl.Rows.Count > 0)
{
string attachment = "attachment; filename=Call-Details-Report-" + startDate.SelectedDate.Value.ToString("MM-dd-yyyy") + ".csv";
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "text/csv";
Response.AddHeader("Pragma", "public");
bool commaRequired = false;
if (this.chkNET_NETWORKID.Checked)
{
Response.Write("Network ID");
commaRequired = true;
}
if (this.chkNET_NETWORKNAME.Checked)
{
if (commaRequired)
{
Response.Write(",");
}
Response.Write("Network");
commaRequired = true;
}
}
In the above code objTbl is my datatable. I read the data from data table and write it to response and get the file for download.but I'm getting an error while I try to download huge files.
Insufficient memory during execution of program. I heard that response.transmit. Can solve this problem but how?
TransmitFile Writes the specified file directly to an HTTP response output stream, without buffering it in memory.
like this :
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-disposition", "attachment; filename=ym.jpg");
context.Response.TransmitFile(context.Server.MapPath(#"~/ym.jpg"));
context.Response.End();