Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 6-Jan-13 19:45pm
GDdixit525
Comments
Orcun Iyigun at 7-Jan-13 0:59am
   
What is the error? Is it something like specified path is not found? Or null reference error?
giri001 at 7-Jan-13 6:04am
   
your code is working fine with both setting.and u get error due to some other reason.
khiladi786 at 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 at 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

Rate this: bad
good
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;
        FileUpload1.SaveAs(img);
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
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Mathew Soji 335
2 BillWoodruff 260
3 OriginalGriff 215
4 Afzaal Ahmad Zeeshan 188
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
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