Hello,
I want to enable the user to upload file from whichever location on the disk the user wants. But when I use the code below, the user can only upload files from the location I have stated in saveas path. I just want to save the file there, but I want it to take the file from wherever user chooses from the disk.
Can anybody help me please how to enable the user to save the file in the stated path in the code, but to select it from whichever location she/he wants? Here is the code
if (this.FileUpload1.HasFile) { Response.Write(this.FileUpload1.FileName); this.FileUpload1.SaveAs(@"path" + this.FileUpload1.FileName); Label5.Text = "File Uploaded: " + this.FileUpload1.FileName; } else { Label5.Text = "No File Uploaded."; }
Thank you
NelPosted Sep 11, 2014, 1:26 PM
Thank you to all of you, I solved it yesterday Here is the solution
Boolean fileOK = false;
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
if (fileExtension == ".csv")
{
fileOK = true;
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(@"path" + this.FileUpload1.FileName);
Label5.Text = "File uploaded!";
}
catch (Exception ex)
{
Label5.Text = "File could not be uploaded.";
}
}
else
{
Label5.Text = "Cannot accept files of this type.";
}
Sanjay KumarPosted Sep 11, 2014, 1:01 AM
try this
protected void btnUpload_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFiles)
{
string path = Server.MapPath("~/Images/");
foreach(HttpPostedFile file in FileUpload1.PostedFiles)
{
try
{
file.SaveAs(Path.Combine(path, file.FileName));
lblOutput.Text += "File Uploaded is : " + file.FileName+"
";
}
catch(Exception ex){
lblOutput.Text="Error happens while uploading your file "+ex.Message;
return;
}
}
}
}
//Multiple File Upload and Save
if (FileUpload1.HasFiles)
{
foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/UploadedFiles/"), uploadedFile.FileName));
}
}
http://www.c-sharpcorner.com/UploadFile/47548d/sending-mail-with-attachment-and-multiple-file-upload-using/
Jignesh TrivediPosted Sep 11, 2014, 12:19 AM
hi,
I think you have to pass proper path to save the file and also IIS user must have right to access this folder.
if (this.FileUpload1.HasFile)
{
Response.Write(this.FileUpload1.FileName);
this.FileUpload1.SaveAs(@"C:\" + this.FileUpload1.FileName);
Label5.Text = "File Uploaded: " + this.FileUpload1.FileName;
}
else
{
Label5.Text = "No File Uploaded.";
}
Please refer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas(v=vs.110).aspx
hope this will help you.
Nitesh KejriwalPosted Sep 10, 2014, 11:58 AM