views:

506

answers:

3

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...

+1  A: 

Create a separate IHttpHandler which would only serve files you want your users to download, and Redirect to that handler in lnkUser_Command.

Anton Gogolev
How can use the IHttpHandler? Is there any example that i can look?Thanks
mehmetserif
+1  A: 

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

Aleris
A: 

TransmitFile method worked very well in my situation,

Thanks a lot Aleris.

mehmetserif