Upload file to folder in asp.net?

if (FileUpload1.HasFile)
            try
            {
                FileUpload1.SaveAs("C:\\Users\\Vinay\\Documents\\Visual Studio 2010\\WebSites\\Onlinedoctorsportal\\vini" + 
                     FileUpload1.FileName);
                Label10.Text = "File name: " +
                     FileUpload1.PostedFile.FileName + "<br>" +
                     FileUpload1.PostedFile.ContentLength + " kb<br>" +
                     "Content type: " +
                     FileUpload1.PostedFile.ContentType;
            }
            catch (Exception ex)
            {
                Label10.Text = "ERROR: " + ex.Message.ToString();
            }
        else
        {
            Label10.Text = "You have not specified a file.";
        }
           //Stream obj = FileUpload1.FileContent;
           //Session["file"] = obj;
           //Response.Redirect("Form3.aspx");
        }
}

I want to save the downloaded file to a folder named vini, but it shows the file but does not save it in the specified folder, as shown, please help

0
source share
3 answers

First, you need to avoid a string literal pointing to a directory

You can do this by adding @ in front of the line or by adding a double backslash.

FileUpload1.SaveAs(@"C:\Users\Vinay\Documents\Visual Studio 2010\WebSites\Onlinedoctorsportal\vini" + FileUpload1.FileName);

OR

FileUpload1.SaveAs("C:\\Users\\Vinay\\Documents\\Visual Studio 2010\\WebSites\\Onlinedoctorsportal\\vini" + FileUpload1.FileName);

Secondly, make sure that the user with whom your ASP.NET application pool is working has write permissions to the specified folder.

A quick check to verify that this is the problem is to issue your local administrator account in the web.config file.

You can do this by customizing the impersonation tag as follows:

<identity impersonate="true"
      userName="domain\user" 
      password="password" />
+3

. ...

-

  protected void Button1_Click(object sender, EventArgs e)
    {
        if (fu1.HasFile)
        {
            String filePath = "~/PDF-Files/" + fu1.FileName;
            fu1.SaveAs(MapPath(filePath));
        }

    }

.

0
    string x = "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\"+FileUpload1.PostedFile.FileName;
    System.Drawing.Image image = System.Drawing.Image.FromFile(x);
    string newPath = FileUpload1.FileName;
    image.Save(Server.MapPath(newPath))    ;
    Image1.ImageUrl = "~//" +  newPath ;
    Image1.DataBind();
0

All Articles