What i want to do is that the user selects some fields on a grid and according to these datas i create an xml file on the web server and then i want user to download it like downloading a any file. But the problem is, i cant use this code:
Response.ContentType = "APPLICATION/OCTET-STREAM";
// initialize the http content-disposition header to
// indicate a file attachment with the default filename
// "myFile.txt"
System.String disHeader = "Attachment; Filename=\"" + fileName +
"\"";
Response.AppendHeader("Content-Disposition", disHeader);
FileInfo downloadFile = new FileInfo(fileFullName);
if (downloadFile.Exists)
{
Response.WriteFile(downloadFile.FullName);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Because i need to let the user download 3 files so the header cannot cary it, what i tought was getting the file names and open a popup, list the file names with a linkbutton, and then the user can download it.
For each file i create a linkbutton at runtime and add this code :
lnkProblem.Text = "Problemler dosyası";
lnkProblem.Visible = true;
lnkProblem.Command += new CommandEventHandler(lnkUser_Command);
lnkProblem.CommandName = Request.QueryString["fileNameProblems"];
lnkProblem.CommandArgument = Request.QueryString["fileNameProblems"];
Then use this function to make it download by the user :
void lnkUser_Command(object sender, CommandEventArgs e)
{
Response.ContentType = "APPLICATION/XML";
System.String disHeader = "Attachment; Filename=\"" + e.CommandArgument.ToString() +
"\"";
Response.AppendHeader("Content-Disposition", disHeader);
FileInfo downloadFile = new FileInfo(Server.MapPath(".") + "\\xmls\\" + e.CommandArgument.ToString());
if (downloadFile.Exists)
{
Response.WriteFile(Server.MapPath(".") + "\\xmls\\" + e.CommandArgument.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Application creates the xml file but somewhere of it, app puts the html tags in that xml file so i can't open the file, is there anyway to do this? Maybe any other example...
The cleaneast way to send a file to the client is to use the TransmitFile method like this:
FileInfo file = new FileInfo(filePath); // full file path on disk
Response.ClearContent(); // neded to clear previous (if any) written content
Response.AddHeader("Content-Disposition",
"attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/xml"; //RFC 3023
Response.TransmitFile(file.FullName);
Response.End();
For multiple files a common solution is to pack all files in a zip file and send them (the mime type would be application/zip in this case).
Create a separate IHttpHandler which would only serve files you want your users to download, and Redirect to that handler in lnkUser_Command.
Related
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.
In ASP.Net (with C#) I'm trying to create a .DAT file with plain text in it and send it to the browser and force download. I've tried several things but I can't get it working. In my aspx-file there is an ImageButton
<asp:ImageButton ID="btnSave" runat="server" CausesValidation="False" ImageUrl="~/Images/Stages/Database/Save.png" OnClick="btnSave_OnClick" Width="26px" />
In the OnClick-method I'm trying to create the file and send it to the browser.
protected void btnSave_OnClick(object sender, EventArgs e)
{
string file = "test.dat";
string fileName = "~\\Stages\\Broekx\\Databanken\\" + file;
FileStream fs = new FileStream(MapPath(fileName), FileMode.Open);
long cntBytes = new FileInfo(MapPath(fileName)).Length;
byte[] byteArray = new byte[Convert.ToInt32(cntBytes)];
fs.Read(byteArray, 0, Convert.ToInt32(cntBytes));
fs.Close();
ImageButton btnSave = (ImageButton)FormViewStagesDummy.FindControl("btnSave");
btnSave.Visible = false;
File.Delete(Server.MapPath(fileName));
if (byteArray != null)
{
this.Response.Clear();
this.Response.ContentType = "text/plain";
this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
this.Response.BinaryWrite(byteArray);
this.Response.End();
this.Response.Flush();
this.Response.Close();
}
}
The file test.dat exists in the correct folder and has to be deleted after it has been read into bytes. I've tried this without deleting the file and that wont work either.
After clicking btnSave the button has to be hidden, so that's why I set the parameter Visible to false.
I've also tried it with content-type "application/octet-stream" or with a PDF file and content-type "application/pdf" but nothing works. The page loads normally and no file is being downloaded.
Is the file string's path actually correct?
this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
Should it not be filename?
Why are you deleting the file before it is written to the response? Would it not make more sense to serve the file via the response and then delete it?
i.e. call
File.Delete(Server.MapPath(fileName));
after the repsonse.
You should try:
Response.TransmitFile( Server.MapPath(fileName) );
Response.End();
TransmitFile is very efficient because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernal cache (based on IIS's caching rules).
Response.End();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename = " + fileName);
Response.TransmitFile(Server.MapPath("~/foldername/" + fileName));
Response.End();
Currently I have the text file going to desktop, in ASP how can I prompt a file save dialog for the user? The result is string from the streamreader as "result" as follows:
StreamWriter FileWriter = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file.txt"));
FileWriter.Write(result);
FileWriter.Close();
String FileName = filename;
String FilePath = filepath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();
Response.AppendHeader("Content-Disposition", "attachment");
StreamWriter FileWriter = new StreamWriter(Response.OutputStream);
FileWriter.Write(result);
I didn't try the code, but maybe you need to omit the call to FileWriter.Close() since it will try to dispose the stream. If not, then you should be using using instead. If it's too problematic, write to the stream directly with its Write method or use a MemoryStream.
An example from one of my apps that does it.
protected void Page_Load(object sender, EventArgs e)
{
string tempFileName = Request["tempFileName"]; // the temp file to stream
string attachFileName = Request["attachFileName"]; // the default name for the attached file
System.IO.FileInfo file = new System.IO.FileInfo(Path.GetTempPath() + tempFileName);
if (!file.Exists)
{
pFileNotFound.Visible = true;
lblFileName.Text = tempFileName;
}
else
{
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the
// Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + attachFileName);
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/octet-stream";
// send the file stream to the client
Response.WriteFile(file.FullName);
}
}
string path = Server.MapPath(your application path);
WebClient client = new WebClient();
byte[] data = client.DownloadData(new Uri(path));
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "aspnet.pdf"));
Response.OutputStream.Write(data, 0, data.Length);
try this code.. its helpful
Don't know if its still open but just to help others
Just use this code it should work to prompt the user to open a dialog for opening or saving the file on the system ....
byte[] bytesPDF = System.IO.File.ReadAllBytes(#"C:\sample.pdf");
if (bytesPDF != null)
{
Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.pdf");
Response.ContentType = "application/octectstream";
Response.BinaryWrite(bytesPDF);
Response.End();
}
If I understand you correctly, you first had a 1-tier (desktop) app that was saving a file using C# to the user's file system. You are now trying to transition to a 2-tier app with your C# code running on the server tier, and the user's browser representing the other tier. There is nothing you can do in C# on the server to write a file to the browser user's file system directly.
That being said, what you need to do is write the file to the HTTP response stream with a content type of something like application/octet-stream. Are you actually using ASP or ASP.NET? If the latter, you should have access to the response stream through a variety of means, but start with the Response object (available from the page, but also available via HttpContext.Current.Response).
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();