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();
}
}
Related
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";
protected void getFileAttachment(byte[] bytes)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = fileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101");
Response.OutputStream.Write(bytes,0,bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
}
I'm trying to convert varbinary that is saved in SQL Server and I'm having trouble because the output file has no extension
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101.filetype");
My fault - I did not include the file extension in my database
Thanks for the comments!
I have base64 string of an image. converting it to byte array & then converting it to memoryStream. How can i download this image to client machine. The base64 string or image is not saved on server/folder/anywhere. It is create dynamically. I have tried
1) Response.ContentType = "image/png" => gives exception obj not set to instant of an object. like this
Context.Response.Clear();
Context.Response.ClearHeaders();
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + registrationId + ".png");
Context.Response.ContentType = "image/jpeg";
Response.AddHeader("Content-Length", bytes.Length.ToString());
//Response.ContentType = "application/octet-stream";
Context.Response.BinaryWrite(bytes);
2) using web client => gives exception
using (WebClient client = new WebClient())
{
client.DownloadFile(ms1.ToString(), registrationId + ".png");
}
Any other solution?
Just try to change
Response.AddHeader("Content-Length", bytes.Length.ToString());
to
Context.Response.AddHeader("Content-Length", bytes.Length.ToString());
I think you doesn't have valid Page object in this context.
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
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);