| | |
upload image
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 35
Reputation:
Solved Threads: 1
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..
wrote this code but the image stored gets corrupt every time..
ASP.NET Syntax (Toggle Plain Text)
protected void Button1_Click(object sender, EventArgs e) { String imagefolder="images"; String path; String savepath; if (FileUpload1.HasFile) { path = Path.Combine(imagefolder, Request.PhysicalApplicationPath); savepath = Path.Combine(path, User .Identity .Name .ToString ()); FileUpload1.SaveAs(savepath); Label1.Text = "Done"; Response.Redirect("userprofile.aspx"); } else { Label1.Text = "Select a file"; } }
•
•
Join Date: Dec 2008
Posts: 104
Reputation:
Solved Threads: 18
Try this:
ASP.NET Syntax (Toggle Plain Text)
protected void Button1_Click(object sender, EventArgs e) { if(FileUpload1.HasFile) { string fileName = FileUpload1.FileName; string fileExtention = fileName.Substring(fileName.LastIndexOf("."), (fileName.Length - fileName.LastIndexOf("."))); string newFileName = User.Identity.Name + fileExtention; string imagefolder=Server.MapPath("~/Images/"); string targetFileName = imagefolder + newFileName; FileUpload1.SaveAs(targetFileName); Label1.Text = "File Uploaded Successfully"; Response.Redirect("userprofile.aspx"); } else { Label1.Text = "Please select a file first"; } }
Falling down is not defeat...
Defeat is when you refuse to getup...
Defeat is when you refuse to getup...
hi,
you can try like this
you can try like this
ASP.NET Syntax (Toggle Plain Text)
string imagefolder =Server.MapPath("Photos"); string path; string savepath; savepath = imagefolder+"\\"+User.Identity.Name.ToString()+System.IO.Path.GetExtension(fileup1.PostedFile.FileName); fileup1.PostedFile.SaveAs(savepath);
If u r query is achieved,mark the thread as solved
Live and Let Live
Live and Let Live
•
•
Join Date: Mar 2009
Posts: 30
Reputation:
Solved Threads: 5
•
•
•
•
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)
protected void Button1_Click(object sender, EventArgs e) { String imagefolder="images"; String path; String savepath; if (FileUpload1.HasFile) { path = Path.Combine(imagefolder, Request.PhysicalApplicationPath); savepath = Path.Combine(path, User .Identity .Name .ToString ()); FileUpload1.SaveAs(savepath); Label1.Text = "Done"; Response.Redirect("userprofile.aspx"); } else { Label1.Text = "Select a file"; } }
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)
protected void Button1_Click(object sender, EventArgs e) { String imagefolder = "images"; String path; String savepath; if (FileUpload1.HasFile) { path = Path.Combine(Request.PhysicalApplicationPath, imagefolder); if (Directory.Exists(path)) { savepath = Path.Combine(path, User.Identity.Name.ToString().Replace(@"\", "_") + "__" + Path.GetFileName(FileUpload1.FileName.ToString())); FileUpload1.SaveAs(savepath); Label1.Text = "Done"; Response.Redirect("userprofile.aspx"); } else { Label1.Text = "Error: file path do not exist"; } } else { Label1.Text = "Select a file"; } }
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.
![]() |
Similar Threads
- Upload image in asp javascript (ASP)
- Upload image from imagefield (ASP.NET)
- How to Upload Image,file (PHP)
- trying to upload image online (myspace, yahoo, etc..) (Windows Vista and Windows 7)
- Upload Image.. (ASP)
- Preview Upload Image Problem (PHP)
- Upload Image Code (HTML and CSS)
Other Threads in the ASP.NET Forum
- Previous Thread: ASP .NET dynamic controls related
- Next Thread: Solve this issue!
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox child class compatible content contenttype control countryselector courier dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownmenu dynamic dynamically edit embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv form gridview gudi iis image javascript list listbox maps menu mobile mouse mssql nameisnotdeclared news novell opera order parent problem radio ratings redirect registration relationaldatabases reportemail schoolproject search security select serializesmo.table sessionvariables silverlight smoobjects software sql sql-server ssl tracking treeview typeof validatedate validation vb.net videos vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming wizard xsl





