[英]File Upload and Download using Asp.net
我正在嘗試使用文件上傳控件上傳,然后使用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();
}
}
}
用這個
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;
}
確保您的驅動器具有讀/寫權限...
Server.MapPath("Files/" + filename)
將位於應用程序執行路徑中的虛擬目錄“Files”(通常是Web應用程序的已編譯DLL所在的“bin”文件夾Server.MapPath("Files/" + filename)
映射到物理路徑。 確保以下內容:
您的“bin”文件夾中有一個“文件”虛擬目錄; 如果路徑意圖在其他地方,例如,在應用程序的根目錄之外,請在代碼中更改它: Server.MapPath("/Files/" + filename)
確保運行Web服務器(IIS?)的用戶對與虛擬目錄對應的物理路徑具有足夠的權限。 通常,這是“IIS_IUSRS”組。 該用戶需要創建/寫入/修改相應物理路徑的權限。
那么你可以試試這個:
fileUpload1.SaveAs(Server.MapPath("~/Files" + filename));
和
lblFilename.Text = "~/Files" + fileUpload1.FileName;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.