简体   繁体   English

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

[英]File Upload and Download using Asp.net

I am trying to use file upload control to upload and then download file using Asp.net C#, But it is giving me a Directory not found exception. 我正在尝试使用文件上传控件上传,然后使用Asp.net C#下载文件,但是它给了我一个找不到目录的例外。 Can anybody help me with this, where I am making the mistake? 在我犯错的地方,有人可以帮助我吗?

Here is my .aspx file: 这是我的.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>

Here is my Code Behind File: 这是我的代码背后文件:

    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();
            }
        }
    }

Use This 用这个

 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;
  }

Make sure your drive had permission for read/write ... 确保您的驱动器具有读/写权限...

Server.MapPath("Files/" + filename) is mapping a Virtual Directory "Files" located in the executing path of your application (typically the "bin" folder where the compiled DLL for your web application resides) to a physical path. Server.MapPath("Files/" + filename)将位于应用程序执行路径中的虚拟目录“Files”(通常是Web应用程序的已编译DLL所在的“bin”文件夹Server.MapPath("Files/" + filename)映射到物理路径。 Ensure the following: 确保以下内容:

  1. You have a "Files" Virtual Directory in your "bin" folder; 您的“bin”文件夹中有一个“文件”虚拟目录; if the path is meant to be elsewhere, for instance, off the root of your application, change it in your code: Server.MapPath("/Files/" + filename) 如果路径意图在其他地方,例如,在应用程序的根目录之外,请在代码中更改它: Server.MapPath("/Files/" + filename)

  2. Make sure the user that your web server (IIS?) is running under has sufficient rights to the physical path that corresponds to the virtual directory. 确保运行Web服务器(IIS?)的用户对与虚拟目录对应的物理路径具有足够的权限。 Typically, this is the "IIS_IUSRS" group. 通常,这是“IIS_IUSRS”组。 That user will need Create / Write / Modify permissions to the corresponding physical path. 该用户需要创建/写入/修改相应物理路径的权限。

Well you can try this : 那么你可以试试这个:

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

And

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM