Why cant opened downloaded docx files ,in asp.net? - asp.net

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.

Related

Opening a word file after downloading from code behind(asp.net)

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

Export a excel file witout any dialog+asp.net,c#

I am exporting a excel file from "c:Test\data.xls" using the responce object
like:
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
it is opening a dialog box.But Now we don't need the dialog box.we want to directly open file without dialog.
Can any body help me out
use
response.AddHeader("Content-Disposition", "inline; filename=" + FileName + ";");
Whether it opens directly depends on client-side security settings to this might work but there is no guarantee...
Instead of attaching file to response you nee write it's content to response stream.
//Set the appropriate ContentType.
Response.ContentType = "application/vnd.ms-excel"; // not sure in content type
//Get the physical path to the file.
string filePath = #"c:Test\data.xls";
//Write the file directly to the HTTP content output stream.
Response.WriteFile(filePath);
Response.End();
But also it depeneds on the client side settings.

ASP.Net Transmit File

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.

Ashx force download image

I have an ashx file that display an image.
I would like not to display the image but force the download.
This is my code :
context.Response.AppendHeader("content-disposition", "attachment; filename=" + userId + ".jpg");
context.Response.ContentType = "image/jpeg";
context.Response.OutputStream.Write(msMasterFinal.ToArray(), 0, msMasterFinal.ToArray().Length);
When I open my browser with this ashx, the image is
automatically displayed. How to force download please ?
Many thanks
Hmm, you code looks right. I've used this snippet in the past to achieve what you're trying to do:
context.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=""{0}""", FileName));
context.Response.AddHeader("Content-Type", FileType);
context.Response.AddHeader("Content-Length", FileSize.ToString);
context.Response.BinaryWrite(FileBytes);
It's almost similar...
Your browser always will show pictures in window if they contentType wiil be "image/jpeg". Try to use different contentType.

How to send a file to a client so a Download dialog will open?

I have a file, say a PDF on my website and when a user visits a page I want to display a download dialog for the pdf on page load or a button click.
I did a google search and I found two ways to do this but wondering what is the accepted way of doing this? I am currently doing this
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader( "content-disposition",
"attachment; filename=" + name );
Response.WriteFile(pdfPath);
Response.End();
(Code was based off code from http://aspalliance.com/259, also found code from
http://www.west-wind.com/weblog/posts/76293.aspx)
Your code, will display the file to the user perfectly. But they will have to use the "Save As" option to actually save it.
If you wish to present the "Save Dialog" to the user, try the following:
string pdfPath = MapPath("mypdf.pdf");
Response.ContentType = "Application/pdf";
Response.AppendHeader("content-disposition",
"attachment; filename=" + pdfPath );
Response.TransmitFile(pdfPath);
Response.End();
This of course assumes the file actually exists on the server and is not being dynamically generated.
This code will Send Any file to directly on client Browser
Response.ContentType = "application/pdf";
Response.WriteFile(PathToFile);
Response.Flush();

Resources