Click here to Skip to main content
13,550,486 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to try to save an image in a folder in my application
Using file upload....but it not working

If i use FileUpload1.Postedfile.FileName.ToString() , than it give error

How can i solve this? ‘ImagesUploaded’ is a folder in my application.

private void StartUpLoad()
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString(); //////LINE 2 or //string
//imgName=FileUpload1.Postedfile.FileName.ToString() this also not working
//give nothing in imgName
//sets the image path
string imgPath = "ImagesUploaded/" + imgName;
//then save it to the Folder

//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;

//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");

pls suggest somethng bros
Posted 6-Jan-13 18:45pm
Orcun-Iyigun 7-Jan-13 0:59am
What is the error? Is it something like specified path is not found? Or null reference error?
Girijesh001 7-Jan-13 6:04am
your code is working fine with both setting.and u get error due to some other reason.
GDdixit 7-Jan-13 7:23am
ok i tried this code now ..

string s11 = FileUpload1.FileName.ToString();
string s2 = Server.MapPath("");
string s3 = s2 + "\\" + "ImagesUploaded" + "\\" + s11;

Image2.ImageUrl = "~/ImagesUploaded/" + s11;

it will working on my page but when i implement this same code in my other
where i want to use do nothing ,i am unable to understand problem...
can u suggest something?
Orcun-Iyigun 7-Jan-13 8:07am
I still do not understand what is the exception since you like a closed box explaining the problem but here is my shot as far as I understand.
HttpContext.Current.Server.MapPath("~\\Images Uploaded\\" + s11);
HttpContext.Current.Server.MapPath(".\\Images Uploaded\\" + s11);

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i have used it once same as u except FileUpload1.FileName.ToString(); this i have used FileUpload1.FileName;

for exmpal

string img = Server.MapPath("/WebSiteName/FolderName_where_To_savePhotos/");
        img += FileUpload1.FileName;

or try

string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                   FileUpload1.PostedFile.SaveAs(Server.MapPath("~/WebSiteName/FolderName_where_To_savePhotos/") + fileName);

u also have take care of same name of photos....for that also use some logic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180515.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100