我正在尝试使用文件上传控件上传然后使用Asp.net C#下载文件,但是它给了我一个找不到目录的例外。我可以帮助我解决这个问题吗?
这是我的.aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploadcheck.aspx.cs" Inherits="fileuploadcheck" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="padding: 20px;">
<tr>
<td>
<asp:Label ID="lblFilename" runat="server" Text="Browse:"></asp:Label>
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="OnLnkUpload" runat="server" OnClick="OnLnkUpload_Click" Font-Underline="False">Upload</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="OnLnkDownload" runat="server" OnClick="OnLnkDownload_Click" Font-Underline="False">Download</asp:LinkButton>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
这是我的代码背后文件:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public partial class fileuploadcheck : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// To save/upload files in folder and download files from folder in asp.net
/// </summary>
string filename = string.Empty;
protected void btnUpload_Click(object sender, EventArgs e)
{
}
protected void OnLnkUpload_Click(object sender, EventArgs e)
{
filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("Files/" + filename));
Response.Write("File uploaded sucessfully.");
lblFilename.Text = "Files/" + fileUpload1.FileName;
}
// To download uplaoded file
protected void OnLnkDownload_Click(object sender, EventArgs e)
{
if (lblFilename.Text != string.Empty)
{
if (lblFilename.Text.EndsWith(".txt"))
{
Response.ContentType = "application/txt";
}
else if (lblFilename.Text.EndsWith(".pdf"))
{
Response.ContentType = "application/pdf";
}
else if (lblFilename.Text.EndsWith(".docx"))
{
Response.ContentType = "application/docx";
}
else
{
Response.ContentType = "image/jpg";
}
string filePath = lblFilename.Text;
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
}
}
答案 0 :(得分:2)
使用此
protected void OnLnkUpload_Click(object sender, EventArgs e)
{
filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("~/Files/" + filename));
Response.Write("File uploaded sucessfully.");
lblFilename.Text = "Files/" + fileUpload1.FileName;
}
确保您的驱动器具有读/写权限...
答案 1 :(得分:1)
Server.MapPath("Files/" + filename)
正在映射虚拟目录&#34;文件&#34;位于应用程序的执行路径(通常是Web应用程序的已编译DLL所在的&#34; bin&#34;文件夹)到物理路径。确保以下内容:
你有一个&#34;文件&#34;您的&#34; bin&#34;中的虚拟目录夹;如果路径意图在其他地方,例如,在应用程序的根目录之外,请在代码中更改它:Server.MapPath("/Files/" + filename)
确保运行Web服务器(IIS?)的用户对与虚拟目录对应的物理路径具有足够的权限。通常,这是&#34; IIS_IUSRS&#34;组。该用户需要创建/写入/修改相应物理路径的权限。
答案 2 :(得分:0)
你可以试试这个:
fileUpload1.SaveAs(Server.MapPath("~/Files" + filename));
和
lblFilename.Text = "~/Files" + fileUpload1.FileName;