Asp.net Webservice returning docx file - asp.net

I have some xml data. I've made an asp.net soap based webservice that takes the xml as a byte buffer applies xsl transformation to it and converts it to a docx file. But when i return the docx file using the response object i get an error that client found response of type application/vnd-word but it was expecting text/xml.
Service Snippet to push document file after xsl transformation
Context.Response.Clear();
Context.Response.ClearHeaders();
Context.Response.ContentType = "application/vnd.openxmlformats- officedocument.wordprocessingml.document";
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.docx");
Context.Response.Flush();
Context.Response.End();

Is this via a webservice call? If you are calling via a webservice proxy client, then it is expecting a SOAP response and not a binary stream. You will need to model a SOAP-compatible response which the caller can understand.
Setting the content-type and content-disposition enables a browser to do the "right" thing (ie open the file-save dialog) but custom clients (httpwebclient, web service proxy, etc) don't have those behaviors built-in so you will need to add whatever is missing.

Where are you actually writing the data?
oh and try this... (if your .docx is a well formed office openxml file)
Response.Clear();
Response.ContentType = "application/msword";
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader("Content-disposition", String.Format("attachment; filename={0}", FileName));
Response.BinaryWrite(DataBytes);
Response.End();
Response.Flush();

Context.Response.Clear();
Context.Response.ContentType = "application/msword";
Context.Response.AddHeader("Content-Type", "application/msword");
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.docx");
Context.Response.End();
Context.Response.Flush();

Related

Calling Generic Handler .ashx from asp.net code behind

I have a generic handler (.ashx) which is used to download an excel file.
When I try to call it using the code below:
HttpWebRequest request = WebRequest.Create("ExcelDownload.ashx?id=123") as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
the file is not downloaded.But,I am able to debug the handler code when the request is made.
But when the handler is used in the anchor tags,I am able to download the file.
Excel Download
Any idea why the first option is not working?
Additional Comments:
In the handler,I am using the below code which works fine with anchor tag.
I am not doing any thing with the response object returned by the request.
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.Buffer = true;
byte[] buffer = memoryStream.ToArray();
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", faWorkbookFileName));
context.Response.BinaryWrite(buffer);
context.Response.End();

download code expalanation in asp.net

can anyone please explain this code
if (e.CommandName == "download")
{
string filename = e.CommandArgument.ToString();
string path = MapPath("~/Docfiles/" + filename);
byte[] bts = System.IO.File.ReadAllBytes(path);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "Application/octet-stream");
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
}
what is command argument, mappath, and also what is this
"Content-Type", "Application/octet-stream"
and also
Response.AddHeader("Content-Length", bts.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.BinaryWrite(bts);
Response.Flush();
First, I advise you to use the MSDN documentation to search for more information about the objects and methods you wanna know more about. MSDN is a useful network and should be used.
Quoting the MSDN CommandArgument : "Gets or sets an optional parameter passed to the Command event along with the associated CommandName". It is used to get an parameter that was passed to the command event. In this case, it was the file name.
string filename = e.CommandArgument.ToString();
The MapPath is used to map a specified path to a physical path. Using this you get the real path of the file. For example: "C:\Docfiles\Yourfile.pdf"
string path = MapPath("~/Docfiles/" + filename);
The ReadAllBytes method, opens a file, reads the content and then closes the file. This return the content of this file as a byte array.
byte[] bts = System.IO.File.ReadAllBytes(path);
The Response object is used to send output to the user from the server.
Response.Clear();
Response.ClearHeaders();
Response.AddHeader is used to build the header of response that will be sent back to user. We use it to set infos about the data we are sending back to the client.
The "Content-Type" attribute is used to especify what kind of file you are returning to the user.
Response.AddHeader("Content-Type", "Application/octet-stream");
The "Content-Length" attribute is used to inform to the browser the size of the the file you are returning.
Response.AddHeader("Content-Length", bts.Length.ToString());
The "Content-Disposition" is used to inform the name of the file that will be returned. For example "file1.doc"
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
The "BinaryWrite()" write your file (who, at this moment, is in a byte array format) to the current HTTP output without any character conversion.
Response.BinaryWrite(bts);
The Flush method sends buffered output immediately.
Response.Flush();
And, finally, causes the server to stop processing the request and return the current result.
Response.End();
If the command is download, most likely from a grid button, then get the name of the file as the argument (these are properties on a button control), and send it to the browser. This prompts the user to download the file.

Why cant opened downloaded docx files ,in 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.

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.

ASP.NET stream content from memory and not from file

The users have requested the option to "download" a csv file representation of GridView contents. Does anyone know how to do this without saving the file to the server but rather just streaming it to the user from memory?
Thanks
Implement an IHttpHandler.
I used something similar to the following in the ProcessResponse for outputing a CSV that had previously been constructed in a database table...
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
//Get data to output here...
//Turn off Caching and enforce a content type that will prompt to download/save.
response.AddHeader("Connection", "close");
response.AddHeader("Cache-Control", "private");
response.ContentType = "application/octect-stream";
//Give the browser a hint at the name of the file.
response.AddHeader("content-disposition", string.Format("attachment; filename={0}", _filename));
//Output the CSV here...
foreach(BatchDTO.BatchRecordsRow row in dtoBatch.BatchRecords)
response.Output.WriteLine(row.Data);
response.Flush();
response.Close();
}
There are a number of libraries that make generating a CSV easier, you should just be able to pass it the Response.OutputStream to have it write to there rather than to a file stream.
Use context.Response.OutputStream.
Here's an example.
I created a StringBuilder and dump the contents to the Response object using the following code ("csv" is the StringBuilder variable).
Response.ContentType = #"application/x-msdownload";
Response.AppendHeader("content-disposition", "attachment; filename=" + FILE_NAME);
Response.Write(csv.ToString());
Response.Flush();
Response.End();
I have used the RKLib export library a few times to great effect, this uses a memory stream and can be given any datatable which it will export as a csv download:
http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx

Resources