Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
1.00/5 (1 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
FileUpload1.SaveAs(Server.MapPath(imgPath));

//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);
}
else
{
//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
Comments
Orcun Iyigun 7-Jan-13 0:59am    
What is the error? Is it something like specified path is not found? Or null reference error?
giri001 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;
FileUpload1.SaveAs(s3);

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 this..it 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);
-or-
HttpContext.Current.Server.MapPath(".\\Images Uploaded\\" + s11);

1 solution

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

for exmpal

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

or try

C#
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
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900