I am trying to export into excel with xlsx format but getting this error when trying to open the file.
Excel cannot open the file 'CertificationMetricsReport.xlsx' because the file format or file extension is not valid. Verify that file has not been corrupted and that the file extension matches the format of the file.
If I will provide the file name CertificationReport.xls then I am able to open it.
Here is the code snippet
public void RenderedReportContent(byte[] fileContent)
{
try
{
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Length", fileContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename="CertificationReport.xlsx");
Response.BinaryWrite(fileContent);
Response.End();
}
catch (Exception ex)
{
if (!(ex is System.Threading.ThreadAbortException))
{
//Other error handling code here
}
}
}
When I am trying to download CertificationMetricsReport.xls it works fine.
Please help me to download the xlsx file.
maybe you can try to change the contenttype
to contenttype = "application/vnd.ms-excel";
or application/octect-stream
Response.Clear();
Response.Buffer = True;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.xslx"
Response.BinaryWrite(YourFile);
Response.End();
also you can try with response.flush
at the end try it in others navigators like IE, Chrome, Firefox...
EDIT: http://social.msdn.microsoft.com/forums/en-US/3d539969-51c4-42bc-8289-6d70d8db7aff/prompt-while-downloading-a-file-for-open-or-save
EDIT2: How to send file in HttpResponse?
Related
I try to download a zip file made by Ionic.Zip.dll from an asp.net c# web form application like this:
zip.AddEntry("filename", arraybyte);
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=SuppliersDocuments.zip");
zip.Save(Response.OutputStream);
Response.Close();
But I get Failed - network error like this:
Error just occurs in chrome and it works properly in another browsers.
Error does not occur on my localhost and it happens only on the main server.
It would be very helpful if someone could explain solution for this problem.
I have the same issue and this happens in Chrome. This issue is happening on Chrome v53 and later which is explained here.
To overcome this issue I suggest you get the length of the file and use Content-Length in the header and pass the length of the file.
string fileName = string.Format("Download.zip");
Response.BufferOutput = false; // for large files
using (ZipFile zip = new ZipFile())
{
zip.AddFile(path, "Download");
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/zip";
Response.AddHeader(name: "Content-Disposition", value: "attachment;filename=" + fileName);
Int64 fileSizeInBytes = new FileInfo(path).Length;
Response.AddHeader("Content-Length", fileSizeInBytes.ToString());
zip.Save(Response.OutputStream);
Response.Flush();
}
Response.Close();
I am looking for code to download a file to a clients pc, I would like to not store the physical file and worry about deleting it at a later stage.
At the moment i create a physical file and then download it, the file name remains the same so it replaces the previous one. My concern is that multiple users might end up overwriting the file when both trying to download at the same time.
Here is some of my code:
using (var package = new ExcelPackage(existingFile))
{
// edit cells and add details into the excel file
//Here u save the file, i would like this to be a stream
using (FileStream aFile = new FileStream(MapPath("../../template/LeadsExport.xlsx"), FileMode.Create))
{
try
{
package.SaveAs(aFile);
aFile.Close();
}
catch (Exception ex)
{
}
}
// Here i download the file, i would like this to use a stream and not a physical file
FileInfo file = new FileInfo(MapPath("../../template/LeadsExport.xlsx"));
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
}
I would like to have the package saved into a file stream and not a physical file and then some way to download it.
You already have half the answer, in both code and in your question-text itself. Instead of writing the Excel package to disk, write it to the response stream.
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=LeadsExport.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
I have created a mdb file,and zipping that file using asp.net and after that I am downloading the zip file.
After downloading file I just want to delete the file from the directory by using c#?
I just tried but the downloading is done after the button click exist but I want to download the file and after downloading it is deleted from the directory.
Have a example:
protected virtual void DownloadNDelete(string sFilePath, string sContentType)
{
string FileName = Path.GetFileName(sFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition","attachment; filename=" + FileName);
Response.ContentType = sContentType;
Response.WriteFile(sFilePath);
Response.Flush();
this.DeleteFile(sFilePath);
Response.End();
}
This may help you
String filename=Directory.GetFile(#"c:\filename");
File.Delete(filename);
Have you tried this code?
string filename = "yourfilename";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
//Content-Disposition will tell the browser how to treat the file.(e.g. in case of jpg file, Either to display the file in browser or download it)
//Here the attachement is important. which is telling the browser to output as an attachment and the name that is to be displayed on the download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
//Telling length of the content..
Response.AddHeader("Content-Length", file.Length.ToString());
//Type of the file, whether it is exe, pdf, jpeg etc etc
Response.ContentType = "application/octet-stream";
//Writing the content of the file in response to send back to client..
Response.WriteFile(file.FullName);
Response.End();
// Delete the file...
System.IO.File.Delete(file.FullName);
}
else
{
Response.Write("This file does not exist.");
}
}
I have designed a website through which when i click a button a .EXE file should download from specific path from my computer.
But its not downloading the exe file instead its downloading the aspx page of the website.
I use the following code:
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myWebClient.DownloadFile("http://localhost:1181/Compile/compilers/sample2.exe", "sample2.exe");
Can you please try this.
string filename = "yourfilename";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
//Content-Disposition will tell the browser how to treat the file.(e.g. in case of jpg file, Either to display the file in browser or download it)
//Here the attachement is important. which is telling the browser to output as an attachment and the name that is to be displayed on the download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
//Telling length of the content..
Response.AddHeader("Content-Length", file.Length.ToString());
//Type of the file, whether it is exe, pdf, jpeg etc etc
Response.ContentType = "application/octet-stream";
//Writing the content of the file in response to send back to client..
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
I hope my edited comment will help to understand. But note: It is just a rough summary. You can do a lot more than this.
I have to implement GEDCOM export in my site.
My .net code created one file at server when export to gedcom clicked.
Then I need to download it to client from server, as well as user should be asked where to save that file, meaning savedialog is required.
After it's downloaded, I want to delete that file from server.
I got one code to transmit file from server to client:
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(Server.MapPath("~/" + FileName));
Response.End();
from this LINK
but I am not able to delete the file after this code as Response.End ends response so whatever code written after that line is not execute.
If I do code to delete file before Response.End();, then file does not transmitted and I get an error.
Anything you put after Response.End won't get executed because it throws a ThreadAbortException to stop execution of the page at that point.
Try this instead:
string responseFile = Server.MapPath("~/" + FileName);
try{
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(responseFile);
Response.Flush();
}
finally {
File.Delete(responseFile);
}
If the file is reasonably small, you can load it into a byte array so that you can delete the file while still being able to send the data:
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
string path = Server.MapPath("~/" + FileName);
byte[] data = File.ReadAllBytes(path);
File.Delete(path);
Response.BinaryWrite(data);
Response.End();