简体   繁体   中英

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. Can anybody help me with this, where I am making the mistake?

Here is my .aspx file:

<%@ 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. Ensure the following:

  1. You have a "Files" Virtual Directory in your "bin" folder; 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)

  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. Typically, this is the "IIS_IUSRS" group. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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