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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.