使用Asp.net进行文件上传和下载

4

我正在尝试使用文件上传控件来上传和下载文件,使用的是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>
                        &nbsp;
                    </td>
                    <td>
                        &nbsp;
                    </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();
            }
        }
    }

受保护的 void OnLnkUpload_Click(object sender, EventArgs e) { filename = Path.GetFileName(fileUpload1.PostedFile.FileName); fileUpload1.SaveAs(Server.MapPath("Files/" + filename)); } - user112647
执行这行代码时会抛出异常。 - user112647
1
将您的Server.MapPath("Files/" + filename)放入一个变量中。然后,在调试期间,您可以查看它的值。它看起来正确吗?您实际上是否有一个名为“Files”的目录,并且该目录对于Web应用程序具有正确的写入权限? - Andrew Morton
@user112647 我投票关闭了这个问题,因为它是由一个简单的排版错误所导致的。 - Andrew Morton
使用 ~/ :fileUpload1.SaveAs(Server.MapPath("~/Files/" + filename)); - Avijit
显示剩余4条评论
3个回答

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

Server.MapPath("Files/" + filename) 将位于您的应用程序执行路径(通常是编译 Web 应用程序的 DLL 文件所在的 "bin" 文件夹)的虚拟目录 "Files" 映射到物理路径。确保以下内容:

  1. 您在 "bin" 文件夹中有一个名为 "Files" 的虚拟目录;如果该路径应位于应用程序根目录之外,请在代码中更改:Server.MapPath("/Files/" + filename)

  2. 确保您的 Web 服务器(IIS?)运行的用户具有足够的权限来访问与虚拟目录相对应的物理路径。通常,这是 "IIS_IUSRS" 组。该用户将需要对相应物理路径具有创建/写入/修改权限。


0

你可以尝试这个:

fileUpload1.SaveAs(Server.MapPath("~/Files" + filename));

而且

   lblFilename.Text = "~/Files" + fileUpload1.FileName;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接