I'm creating an excel file using Respose.Write in ASP.NET C#, my users can save the created file on their systems, but I want to save this excel file on my server, without user knowing anything about it. how is it possible? this is how I create my Excel file:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
....
Response.Write("</table>");
Response.Flush();
Response.End();
this code saves created excel file client PC, but I want to save created file on the server
The general idea is to write the contents of the Excel file to a buffer. Then a) save it to a file and b) stream it to the client. You will want to use a unique file naming convention on the server (include a time stamp for example). The IIS App Pool also needs write permission on the folder where you are saving the file to.
StringBuilder excel = new StringBuilder();
excel.Append("<table>");
...
excel.Append("</table>");
// save to file
File.WriteAllText("C:\\blah.xls", excel.ToString());
// output to response
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(excel.ToString());
Response.End();
Related
I want to download the parent Zip file which contains more zip files in asp.net c#.
Following code work for single zip file which have more .pdf,.txt ect.. files.
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddEntry(filename, "");
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + "filename.zip");
zip.Save(Response.OutputStream);
Response.End();
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);
}
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.
Suppose that I want my ASP.net web server to open a file and then send it to the browser.
First, I write this:
FileInfo file = new System.IO.FileInfo(#"\\myshare\myfile.zip");
FileStream fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
What comes next?
I would think something along the lines of Response.Write(..., but I'm having trouble figuring it out.
straight from MSDN:
HttpResponse.BinaryWrite Method
FileStream MyFileStream;
long FileSize;
MyFileStream = new FileStream("sometext.txt", FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
MyFileStream.Read(Buffer, 0, (int)FileSize);
MyFileStream.Close();
Response.Write("<b>File Contents: </b>");
Response.BinaryWrite(Buffer);
Edit: of course there are also many other methods, like streaming which allows you to never allocate the byte[] buffer all at once on the web server. This was just a starting point...
A good way to send files is using the content-disposition header before you send the actual raw data
for instance:
Response.ContentType = "application/jpeg";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.WriteFile(path);
Response.End();
where fileName is just the filename (with extension)
and path is the full path to the file
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();
The actual zip file c:\temp\test.zip is good, valid, whatever you want to call it. When I navigate to the directory c:\temp\ and double-click on the test.zip file; it opens right up.
My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can chose to either save or open. If I try to open the file from the dialog, or save it and then open it. I get the following dialog message:
The Compressed (zipped) Folder is invalid or corrupted.
For Response.ContentType I've tried:
application/x-compressed
application/x-zip-compressed
application/x-gzip-compresse
application/octet-stream
application/zip
The zip file is being created with some prior code (that I'm sure is working fine due to my ability to open the created file directly) using: Ionic.zip
http://www.codeplex.com/DotNetZip
This worked. I don't know why but it did.
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
//Response.ClearContent();
//Response.ClearHeaders();
//Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.AppendHeader("Content-Cength", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
//Response.Flush();
Response.End();