ASP.NET - AJAX UpdatePanel Save As Dialog Problem

Asked By Henry Taylor on 13-Oct-14 04:33 PM
There is something about how paths are resolved that I just don't get.

On my home machine I can run code that references a directory under the project and everything runs fine.  For instance I can have a folder named "docs" and have a PDF file there and open it through a call.

Replicating that directory under my root on my WWW.realWorld website also works.

At work the machine I have tries to go to C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7

If I put the files in that path the code still can't "see" them.

What I am trying to do is provide a link in a gridview cell to a ,csv file that already exists and allow the user to download that file to their machine.
Robbe Morris replied to Henry Taylor on 13-Oct-14 10:27 AM
A url path does not resolve to a local disk path.  A url path only knows folder structures from the root of the web site. 

If you place a file in c:\inetpub\wwwroot\mysite\files\rob.csv and the root folder of the web site is "mysite", then the url is
http://www.mywebsitedomainname.com/files/rob.csv
Henry Taylor replied to Robbe Morris on 13-Oct-14 12:30 PM
I think I have resolved the path problem but something else comes up.

I removed the AJAX UpdatePanel and now the SaveAs dialog opens but it tries to Save the file as an ASPX page. I can rename the file with the correct extension and it works but that is not what we want a user to have to do.

Does anyone know why this happens?

Here is the code.  To make this easier I have hard-coded some of the path for now:

string FileName = @"docs\Oct_2014\2014_Oct_Monthy_Report.xls";
string FilePath = Server.MapPath("~") + FileName;
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + FileName);
Response.WriteFile(FilePath);
Response.End();

In addition the file name is the name of the aspx page. Bad web site bad.

Robbe Morris replied to Henry Taylor on 13-Oct-14 12:33 PM
I don't think this works with an AJAX UpdatePanel.  I seemed to remember running into this before and did a search for you.

http://stackoverflow.com/questions/10014920/response-write-and-updatepanel
Henry Taylor replied to Robbe Morris on 13-Oct-14 01:22 PM
Right.

I added a <Trigger> tag

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>        
            <asp:PostBackTrigger ControlID="myGridView" />
        </Triggers>

and now the SaveAs() dialog opens but it has the PageName.aspx for the filename.  I can change the name of the file and the extension and it works.

Any ideas about the the filename problem?

Thanks.
Robbe Morris replied to Henry Taylor on 13-Oct-14 01:38 PM
They only thing I can see here is that your filename: attribute is populated with a filename that includes the folders and not just the actual .Name of the file. 
Henry Taylor replied to Robbe Morris on 13-Oct-14 01:44 PM
Sorry about that.  I have changes:

string FileName = e.CommandArgument.ToString();
string FilePath = Server.MapPath("~") + FileName; << this FileName has some Path info in it
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition:", "attachment; filename=" + Path.GetFileName(FileName));
Response.TransmitFile(FilePath);
Context.ApplicationInstance.CompleteRequest();

Works with AJAX call but tries to name file as PageName and FileType = .aspx

If I change the file extension in the SaveAs() dialog it writes the file out correctly so it know what to do, just can't get teh file name and extension right.
Henry Taylor replied to Robbe Morris on 13-Oct-14 03:37 PM
I got it.  I needed to add the Encoding stuff.

string FileName = e.CommandArgument.ToString();
string FilePath = Server.MapPath("~") + FileName;
Response.ClearHeaders();
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(Path.GetFileName(FileName)));
Response.AddHeader("Content-Type", "application/Excel");
Response.TransmitFile(FilePath);

Of course as we all know computers are alive and they hate us. As soon as they figure out that we know what they know they will change the rules and nothing will work.

Neo help us all.