Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 18th, 2008, 06:16 AM
Authorized User
 
Join Date: Jul 2008
Location: new delhi, delhi, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav gupta
Default prob. related to image 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


  #2 (permalink)  
Old July 18th, 2008, 07:59 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default

If i understand you right... no matter if you put two files or one file your loop still tries to upload two? if that's the case then you need to check that the HttpPostedFile has a file. If it does then upload it otherwise do nothing:

 {
     string filepath = "C:\\Uploads";
     HttpFileCollection uploadedFiles = Request.Files;
     for (int i = 0; i <= uploadedFiles.Count - 1; i++) {
         HttpPostedFile userPostedFile = uploadedFiles(i);
         if (userpostedfile.hasfile) {
             userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName ));
         }
     }
 }



Jason Hall
  #3 (permalink)  
Old July 21st, 2008, 07:50 AM
Authorized User
 
Join Date: Jul 2008
Location: new delhi, delhi, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav gupta
Default

thanks




Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
how to upload image? vipin k varghese BOOK: XSLT Programmer's Reference, 2nd Edition 3 June 3rd, 2008 02:53 AM
Upload image phuc2583 Classic ASP Basics 1 January 17th, 2008 08:48 AM
Image Upload spbharti Pro PHP 3 February 3rd, 2006 08:20 AM
Image upload cutesneakers PHP How-To 2 January 5th, 2005 08:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.