0

I have this function:

    function PrintCApplication() {
        cApplication.PrintApplication(applicationID,
             function (isSuccessful, saveFileLocation) {
                 if (isSuccessful) {
                     document.location = '/CApplication/DownloadFile/' + saveFileLocation;
                 }
             });
     };

saveFileLocation is a file path in my directory. I want to pass it as string via document.location because I want to download it but when I do it returns a bad request(I guess this is because of backslashes).

saveFileLocation value is C:/Users/Me/Documents/CApplicationTemplate.xlsx

This is my sample url that is not working. http://localhost:4617/CApplication/DownloadFile/C:/Users/Me/Documents/CApplicationTemplate.xlsx

QUESTION: Is there a way that I could pass a file path via document.location as string? If Yes how? If No, Is there other way?

bot
  • 4,841
  • 3
  • 42
  • 67

2 Answers2

1

You can only redirect to a relative path using javascript:

window.location.href = '/MyWebsite/DownloadFile/' + saveFileLocation;

Note this is a relative path to your web application directory. You cannot use javascript to redirect to a C:// drive file location. Explanation of why is here: Go to local URL with Javascript

Community
  • 1
  • 1
James Dev
  • 2,979
  • 1
  • 11
  • 16
1

Assuming you are using IIS (Visual Studio)

You can access only files exposed under your root path website Server.MapPath("~") and adding .xlsx headers in your web.config file.

  1. Solution One : Put your file under your root path and link with Server.MapPath("~/mySubfolder/CApplicationTemplate.xlsx").
  2. Solution Two: Handle your file in code behind with something like:

In WebForm:

<asp:Button OnClick="btnExcel_Click" />/

In Code Behind :

private void btnExcel_Click(object sender, System.EventArgs e)
{
    var fileName = "C:/Users/Me/Documents/CApplicationTemplate.xlsx";
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    using (var fs = File.OpenRead(fileName))
    {
        ms.SetLength(fs.Length);
        fs.Read(ms.GetBuffer(), 0, Convert.ToInt32(fs.Length));
    }

    //Clear headers
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    //Add my send my xlsx file
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
    Response.AddHeader("Content-Disposition", "attachment; filename=CApplicationTemplate.xlsx");
    Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Expires", "0");
    ms.WriteTo(Response.OutputStream);

    Response.Flush();
    ms.Dispose();
}

Add the folowing config in your web.config file :

<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".xslx" mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
        </staticContent>
    </system.webServer>
</configuration>
Rogerio Soares
  • 1,613
  • 16
  • 14