Save Uploaded File in specific location using C# and VB.Net in ASP.Net

Last Reply on Nov 06, 2017 06:40 AM By kalpesh

Posted on Nov 06, 2017 03:14 AM

Hai All,
i have a query in my web application. in my application by using fileupload i want to attach file from network system and send to customer and the attached file should be in local system and not store in my web application folder.

if you understand my problem please help me with example

 

Thanks in advance

Posted on Nov 06, 2017 06:40 AM Modified on on Nov 06, 2017 06:41 AM

Refer below code you need call code in your Event where you are sending email with an attachment file. Refer the below code and implement it as per your logic.

C#

// Call this code Where your are sending email at same time you can save file

//string folderPath = Server.MapPath("~/Files/");
string folderPath = @"E:\Images\"; // Your path Where you want to save other than Server.MapPath
//Check whether Directory (Folder) exists.
if (!Directory.Exists(folderPath))
{
    //If Directory (Folder) does not exists. Create it.
    Directory.CreateDirectory(folderPath);
}

//Save the File to the Directory (Folder).
FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));

VB.Net

' Call this code Where your are sending email at same time you can save file

'string folderPath = Server.MapPath("~/Files/");
Dim folderPath As String = "E:\Images\"
' Your path Where you want to save other than Server.MapPath
'Check whether Directory (Folder) exists.
If Not Directory.Exists(folderPath) Then
	'If Directory (Folder) does not exists. Create it.
	Directory.CreateDirectory(folderPath)
End If

'Save the File to the Directory (Folder).
FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))