problem related to picture upload
-
hi every one i am using this code to upload two images or you can upload no of images from this code . string filepath = "C:\\Uploads"; HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); } but whan i want to load only one image then it shows error: Could not find a part of the path 'C:\Uploads\'. when i try to search that what is going on.then i came to know that if i select 1 image only then the value of uploadedFiles 2 that why loop run two times and show error . my prob is why it takes 2 when i select 1 image only. plz let me know. thanks ...gaurav
-
hi every one i am using this code to upload two images or you can upload no of images from this code . string filepath = "C:\\Uploads"; HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); } but whan i want to load only one image then it shows error: Could not find a part of the path 'C:\Uploads\'. when i try to search that what is going on.then i came to know that if i select 1 image only then the value of uploadedFiles 2 that why loop run two times and show error . my prob is why it takes 2 when i select 1 image only. plz let me know. thanks ...gaurav
gaurav mangal wrote:
came to know that if i select 1 image only then the value of uploadedFiles 2
Even if you are selecting one image, Request.Files will return the total upload control count. So you need to check the content length before you save the file.
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
if(userPostedFile.ContentLength != 0){
userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
}
}gaurav mangal wrote:
string filepath = "C:\\Uploads";
Use
Server.MapPath
always. ASP.NET won't have access to other directories in the system.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
hi every one i am using this code to upload two images or you can upload no of images from this code . string filepath = "C:\\Uploads"; HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); } but whan i want to load only one image then it shows error: Could not find a part of the path 'C:\Uploads\'. when i try to search that what is going on.then i came to know that if i select 1 image only then the value of uploadedFiles 2 that why loop run two times and show error . my prob is why it takes 2 when i select 1 image only. plz let me know. thanks ...gaurav
try this hope it works for (int i = 0; i < uploadedFiles.Count-1; i++)
-
try this hope it works for (int i = 0; i < uploadedFiles.Count-1; i++)
metallica_rock10 wrote:
try this hope it works for (int i = 0; i < uploadedFiles.Count-1; i++)
NO. It ignores the last file.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
gaurav mangal wrote:
came to know that if i select 1 image only then the value of uploadedFiles 2
Even if you are selecting one image, Request.Files will return the total upload control count. So you need to check the content length before you save the file.
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
if(userPostedFile.ContentLength != 0){
userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));
}
}gaurav mangal wrote:
string filepath = "C:\\Uploads";
Use
Server.MapPath
always. ASP.NET won't have access to other directories in the system.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
hi Navaneeth thanks for the advice .its really works. thanks a lot gaurav
-
hi every one i am using this code to upload two images or you can upload no of images from this code . string filepath = "C:\\Uploads"; HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); } but whan i want to load only one image then it shows error: Could not find a part of the path 'C:\Uploads\'. when i try to search that what is going on.then i came to know that if i select 1 image only then the value of uploadedFiles 2 that why loop run two times and show error . my prob is why it takes 2 when i select 1 image only. plz let me know. thanks ...gaurav
for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; if(userPostedFile.HasFile) { userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); } }
Pradeep Reddy