upload file to a folder in asp.net?

2019-04-15 23:38发布

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

what i want is to save the uploaded file to a folder named vini but it is showing the file but not saving it to the specified folder as shown please help

3条回答
冷血范
2楼-- · 2019-04-16 00:04

Firstly you need to escape your string literal that points to the directory

You can do this by adding an @ before the string, or by putting double backslashes.

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, check that the user that your ASP.NET application pool process is running under has permissions to write to the specified folder.

A quick check to see if this is the problem is to impersonate your local admin account in your web.config file.

You can do this by configuring the impersonate tag as follows:

<identity impersonate="true"
      userName="domain\user" 
      password="password" />
查看更多
趁早两清
3楼-- · 2019-04-16 00:08
    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();
查看更多
ら.Afraid
4楼-- · 2019-04-16 00:22

This is your answer Try it....

This is Button click event Code -

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

    }

I this will solve your problem.

查看更多
登录 后发表回答