SearchForKnowledge
SearchForKnowledge

Reputation: 3751

Why is downloading a file failing from the server

I set up a website project in VS2012 and created a folder within the project, FileStore. I created an IIS site which points to the project folder.

I have a file saved in the FileStore folder which I want to allow the user to download from anywhere within the network.

The structure: project folder\FileStore\myFile.dat

I have an ASP.net page in project folder\ where I have a button to download the myFile.dat file:

<asp:Button ID="Button3" runat="server" Text="Download File" OnClick="Button3_Click" Width="146px" Height="26px"  />

C#:

protected void Button3_Click(object sender, EventArgs e)
{

    // Download File Button after SP SSIS Job places it in the MLINT\files\ folder.
    if (File.Exists("FileStore\\myFile.DAT")) {
        Response.ContentType = "data/dat";
        Response.AppendHeader("Content-Disposition", "attachment; filename=myFile.DAT");
        Response.TransmitFile("FileStore\\myFile.DAT");
        Response.End();
    }
    else
    {
        lblMessage.Text = "File doesn't exist in the system.";
        lblMessage.CssClass = "fontRed";
    }
}

I keep receiving the File doesn't exist in the system. message.

How can I resolve the issue.

Upvotes: 0

Views: 115

Answers (1)

Eric J.
Eric J.

Reputation: 150108

You need to map the path to the path used by the web server

string actualPath = Server.MapPath("~\\FileStore\\myFile.DAT");
if (File.Exists(actualPath))

Upvotes: 3

Related Questions