Binary to File download - But the downloaded file has no file extension? - c#

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!

Related

excel cannot open the file because the file format or file extension is not valid c#

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";

Byte array not writing to pdf stream c#

I've have a byte array that has all the data needed and can be displayed in a pdf that can be opened if I use this code:
System.IO.File.WriteAllBytes(#"C:\Users\Person\Downloads\results.pdf", bytes)
That let's me know the byte array should be good. The issue is I'm trying to create a pdf on the fly and when I view the call in the network tab in dev tools, it shows a 200 and in the response tab I get a text response. When I copy that response in a text editor and save it as a pdf, it has the correct amount of pages, but no data in it. So I have two issues here. For some reason, it is not being returned as a pdf AND the byte array is not being written to it. Any suggestions on what I can do? I've looked at stack overflow all day and have tried all kinds of things including setting the contentType to application/pdf.
byte[] bytes = ms.ToArray();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
response.AddHeader("Accept-Header", bytes.Length.ToString());
response.AddHeader("Content-Length", bytes.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
response.AddHeader("Content-Description", "File Download");
response.ContentType = "application/octet-stream";
response.OutputStream.Write(bytes, 0, onvert.ToInt32(bytes.Length));
//I've tried using this as well
//response.BinaryWrite(bytes);
response.Flush();
response.End();
UPDATED CODE:
byte[] bytes = ms.ToArray();
HttpResponse response = HttpContext.Current.Response;
try
{
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.AddHeader("Accept-Header", bytes.Length.ToString());
response.AddHeader("Content-Length", bytes.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
response.ContentType = "application/pdf";
response.BinaryWrite(bytes);
response.Flush();
}
catch { }
finally
{
response.End();
}
I got it working with this:
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/pdf";
response.AddHeader("content-disposition", "attachment; filename=Results.pdf");
response.BinaryWrite(bytes);
response.Flush();
response.Close();

TransmitFile is not working

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

Word and Excel files not getting downloaded in my asp.net website

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);

Convert .aspx page to Microsoft Excel Page

HI i want to use such thing in my web application that on a button click .aspx page converted in Xls page.
I did it but result is not good as I needed. I used this code on button click which is given below.
Response.Clear();
Response.Buffer = true;
string filename = "TicketSecretary.xlxs";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
I'm missing the Response.End(); at the end.
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.end.aspx

Categories