Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. problem related to picture upload

problem related to picture upload

Scheduled Pinned Locked Moved ASP.NET
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gaurav mangal
    wrote on last edited by
    #1

    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

    N M P 3 Replies Last reply
    0
    • G gaurav mangal

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      G 1 Reply Last reply
      0
      • G gaurav mangal

        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

        M Offline
        M Offline
        metallica_rock10
        wrote on last edited by
        #3

        try this hope it works for (int i = 0; i < uploadedFiles.Count-1; i++)

        http://aspdotnetnstuff.blogspot.com

        N 1 Reply Last reply
        0
        • M metallica_rock10

          try this hope it works for (int i = 0; i < uploadedFiles.Count-1; i++)

          http://aspdotnetnstuff.blogspot.com

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • N N a v a n e e t h

            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

            G Offline
            G Offline
            gaurav mangal
            wrote on last edited by
            #5

            hi Navaneeth thanks for the advice .its really works. thanks a lot gaurav

            1 Reply Last reply
            0
            • G gaurav mangal

              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

              P Offline
              P Offline
              pradeep kumarappagari
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups