upload image

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 35
Reputation: bharatshivram is an unknown quantity at this point 
Solved Threads: 1
bharatshivram bharatshivram is offline Offline
Light Poster

upload image

 
0
  #1
Mar 18th, 2009
hi, i want the name of the image selected by a user when uploading an image to be changed to his username.
wrote this code but the image stored gets corrupt every time..

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. String imagefolder="images";
  4. String path;
  5. String savepath;
  6.  
  7.  
  8. if (FileUpload1.HasFile)
  9. {
  10. path = Path.Combine(imagefolder, Request.PhysicalApplicationPath);
  11.  
  12. savepath = Path.Combine(path, User .Identity .Name .ToString ());
  13.  
  14. FileUpload1.SaveAs(savepath);
  15.  
  16. Label1.Text = "Done";
  17.  
  18. Response.Redirect("userprofile.aspx");
  19. }
  20. else
  21. {
  22. Label1.Text = "Select a file";
  23. }
  24. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 104
Reputation: Aneesh_Argent is an unknown quantity at this point 
Solved Threads: 18
Aneesh_Argent Aneesh_Argent is offline Offline
Junior Poster

Re: upload image

 
1
  #2
Mar 19th, 2009
Try this:

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. if(FileUpload1.HasFile)
  4. {
  5. string fileName = FileUpload1.FileName;
  6. string fileExtention = fileName.Substring(fileName.LastIndexOf("."), (fileName.Length - fileName.LastIndexOf(".")));
  7. string newFileName = User.Identity.Name + fileExtention;
  8. string imagefolder=Server.MapPath("~/Images/");
  9. string targetFileName = imagefolder + newFileName;
  10. FileUpload1.SaveAs(targetFileName);
  11. Label1.Text = "File Uploaded Successfully";
  12. Response.Redirect("userprofile.aspx");
  13. }
  14. else
  15. {
  16. Label1.Text = "Please select a file first";
  17. }
  18. }
Falling down is not defeat...
Defeat is when you refuse to getup...
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: upload image

 
1
  #3
Mar 19th, 2009
hi,

you can try like this

  1. string imagefolder =Server.MapPath("Photos");
  2. string path;
  3. string savepath;
  4. savepath = imagefolder+"\\"+User.Identity.Name.ToString()+System.IO.Path.GetExtension(fileup1.PostedFile.FileName);
  5.  
  6. fileup1.PostedFile.SaveAs(savepath);
If u r query is achieved,mark the thread as solved

Live and Let Live
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 30
Reputation: P.K.Chaudhary is an unknown quantity at this point 
Solved Threads: 5
P.K.Chaudhary P.K.Chaudhary is offline Offline
Light Poster

Re: upload image

 
0
  #4
Mar 20th, 2009
Originally Posted by bharatshivram View Post
hi, i want the name of the image selected by a user when uploading an image to be changed to his username.
wrote this code but the image stored gets corrupt every time..

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. String imagefolder="images";
  4. String path;
  5. String savepath;
  6.  
  7.  
  8. if (FileUpload1.HasFile)
  9. {
  10. path = Path.Combine(imagefolder, Request.PhysicalApplicationPath);
  11.  
  12. savepath = Path.Combine(path, User .Identity .Name .ToString ());
  13.  
  14. FileUpload1.SaveAs(savepath);
  15.  
  16. Label1.Text = "Done";
  17.  
  18. Response.Redirect("userprofile.aspx");
  19. }
  20. else
  21. {
  22. Label1.Text = "Select a file";
  23. }
  24. }
[]
Hi dear,

Your code is correct accept few problems related to it:
1. The code path = Path.Combine(imagefolder, Request.PhysicalApplicationPath); creates a wrong path, please chnage it code to
path = Path.Combine(Request.PhysicalApplicationPath, imagefolder);
2. The file name (Username in this case) is missing the extension, so when system try to save file it would generate errors.
3. Please check path should actually exist before you save file.
4. The username can have special charachers like '\' which need to be removed.

rest of your code is fine.
You can use following code for reference

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. String imagefolder = "images";
  4. String path;
  5. String savepath;
  6.  
  7.  
  8. if (FileUpload1.HasFile)
  9. {
  10. path = Path.Combine(Request.PhysicalApplicationPath, imagefolder);
  11. if (Directory.Exists(path))
  12. {
  13. savepath = Path.Combine(path, User.Identity.Name.ToString().Replace(@"\", "_") + "__" + Path.GetFileName(FileUpload1.FileName.ToString()));
  14. FileUpload1.SaveAs(savepath);
  15. Label1.Text = "Done";
  16. Response.Redirect("userprofile.aspx");
  17. }
  18. else
  19. {
  20. Label1.Text = "Error: file path do not exist";
  21. }
  22.  
  23. }
  24. else
  25. {
  26. Label1.Text = "Select a file";
  27. }
  28. }
Last edited by peter_budo; Mar 21st, 2009 at 3:24 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC