为什么从服务器下载文件失败
Why is downloading a file failing from the server
我在VS2012中建立了一个网站项目,并在项目中创建了一个文件夹,FileStore
。我创建了一个指向项目文件夹的 IIS 站点。
我在 FileStore
文件夹中保存了一个文件,我希望用户可以从网络中的任何位置下载该文件。
结构:project folder\FileStore\myFile.dat
我在 project folder\
中有一个 ASP.net 页面,其中有一个按钮可以下载 myFile.dat
文件:
<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";
}
}
我一直收到 File doesn't exist in the system.
消息。
我该如何解决这个问题。
您需要将路径映射到网络服务器使用的路径
string actualPath = Server.MapPath("~\FileStore\myFile.DAT");
if (File.Exists(actualPath))
我在VS2012中建立了一个网站项目,并在项目中创建了一个文件夹,FileStore
。我创建了一个指向项目文件夹的 IIS 站点。
我在 FileStore
文件夹中保存了一个文件,我希望用户可以从网络中的任何位置下载该文件。
结构:project folder\FileStore\myFile.dat
我在 project folder\
中有一个 ASP.net 页面,其中有一个按钮可以下载 myFile.dat
文件:
<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";
}
}
我一直收到 File doesn't exist in the system.
消息。
我该如何解决这个问题。
您需要将路径映射到网络服务器使用的路径
string actualPath = Server.MapPath("~\FileStore\myFile.DAT");
if (File.Exists(actualPath))