773 views
asked in Asp.net C# by
Asp.net Tutorial - Upload a pdf/file in network share drive in asp.net c#

1 Answer

answered by

Uploading a PDF to a network share drive in ASP.NET C# involves creating a web form to accept the file upload, saving the file to a specified directory, and ensuring the application has the necessary permissions to access the network share. Here is a step-by-step guide:

1. Create the Web Form:

  • Add a web form to your ASP.NET project.
  • Include a file upload control and a button to submit the form.

---- Create page WebForm.aspx

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

2. Handle the File Upload in Code-Behind:

  • In the code-behind file (.aspx.cs), handle the file upload when the button is clicked.
  • Save the uploaded file to the network share directory.

-- WebForm.aspx.cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace WebApplication7
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                try
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    string networkPath = @"\\192.168.32.32\AppData_Upload\" + filename;

                    // Save the file to the network path
                    FileUpload1.SaveAs(networkPath);

                    Label1.Text = "File uploaded successfully!";
                }
                catch (Exception ex)
                {
                    Label1.Text = "Error: " + ex.Message;
                }
            }
            else
            {
                Label1.Text = "Please select a file to upload.";
            }
        }
    }
}
  • 3. Ensure Permissions:

    • Make sure the application pool identity or the user account running the application has write permissions to the network share directory.
    • You can set this in the network share properties by adding the appropriate user and giving it the necessary permissions.
  • 4. Web.Config Settings (if necessary):

    • If you need to use impersonation to access the network share, you can set it in the web.config file.
<configuration>
  <system.web>
    <identity impersonate="true" userName="domain\username" password="password" />
  </system.web>
</configuration>
  • By following these steps, you can create a simple ASP.NET web form that allows users to upload a PDF file to a network share directory. Make sure to handle exceptions and edge cases as needed for your specific use case.

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...