vini
vini

Reputation: 4722

upload file to a 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");
        }
}

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

Upvotes: 1

Views: 17057

Answers (3)

BHUSHAN MAHAJAN
BHUSHAN MAHAJAN

Reputation: 1

    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();

Upvotes: 0

Arvind Dhakar
Arvind Dhakar

Reputation: 1

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.

Upvotes: 0

Spencer Rose
Spencer Rose

Reputation: 1230

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" />

Upvotes: 5

Related Questions