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