943,542 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 1846
  • ASP.NET RSS
Mar 18th, 2009
0

upload image

Expand 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..

ASP.NET Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
bharatshivram is offline Offline
35 posts
since Mar 2007
Mar 19th, 2009
1

Re: upload image

Try this:

ASP.NET Syntax (Toggle Plain Text)
  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. }
Reputation Points: 16
Solved Threads: 18
Junior Poster
Aneesh_Argent is offline Offline
104 posts
since Dec 2008
Mar 19th, 2009
1

Re: upload image

hi,

you can try like this

ASP.NET Syntax (Toggle Plain Text)
  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);
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007
Mar 20th, 2009
0

Re: upload image

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..

ASP.NET Syntax (Toggle Plain Text)
  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

ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 5
Light Poster
P.K.Chaudhary is offline Offline
30 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: ASP .NET dynamic controls related
Next Thread in ASP.NET Forum Timeline: Solve this issue!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC