locked
File Upload is saying not connected to a network when using Multiple="Multiple" option RRS feed

  • Question

  • User2103134756 posted

    I downloaded some examples of the FileUpload control using the Multiple="Multiple" option. My system is not connected to the internet or a local server. Do I need to configure IIS or something so I can test this?

    Example 1

    <head runat="server">
        <title></title>
    </head>
    <body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <input type="file" id="myfile" multiple="multiple" name="myfile" runat="server" size="100" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <br />
        <asp:Label ID="Span1" runat="server"></asp:Label>
    </div>
    </form>
    </body>
    </html>
    
    protected void Button1_Click(object sender,EventArgs e) {
    	  string filepath = Server.MapPath("\\Upload");
    	  HttpFileCollection uploadedFiles = Request.Files;
    	  Span1.Text = string.Empty;
    
    	  for(int i = 0;i < uploadedFiles.Count;i++) {
    		  HttpPostedFile userPostedFile = uploadedFiles[i];
    
    		  try {
    			  if (userPostedFile.ContentLength > 0) {
    				 Span1.Text += "<u>File #" + (i + 1) +  "</u><br>";
    				 Span1.Text += "File Content Type: " +  userPostedFile.ContentType      + "<br>";
    				 Span1.Text += "File Size: " + userPostedFile.ContentLength           + "kb<br>";
    				 Span1.Text += "File Name: " + userPostedFile.FileName + "<br>";
    
    				 userPostedFile.SaveAs(filepath + "\\" +    Path.GetFileName(userPostedFile.FileName));                  
    				 Span1.Text += "Location where saved: " +   filepath + "\\" +   Path.GetFileName(userPostedFile.FileName) + "<p>";
    			  }
    		  } catch(Exception Ex) {
    			  Span1.Text += "Error: <br>" + Ex.Message;
    		  }
    	   }
    	}
    }

    Example 2

    <asp:FileUpload ID="fileImages" Multiple="Multiple" runat="server" />
    
    protected btnUpload_Click(object sender, EventArgs e)
    {
         HttpFileCollection flImages = Request.Files;
         foreach (string key in flImages)
         {
           HttpPostedFile flfile = flImages[key];
           flfile.SaveAs("~/Upload2/" + flfile.FileName);
          }
    }
    
    
    

    Monday, July 22, 2019 4:00 AM

Answers

  • User-719153870 posted

    Hi oneillj,

    The code you provided work fine in my demo. The problem may be that you can't connect to localhost when you debug.

    Please check if you have IIS Express like below in your visual studio:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 2:16 AM

All replies

  • User-719153870 posted

    Hi oneillj,

    The code you provided work fine in my demo. The problem may be that you can't connect to localhost when you debug.

    Please check if you have IIS Express like below in your visual studio:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 23, 2019 2:16 AM
  • User2103134756 posted

    Looks like that did it. I also had to add the following line in the web.config file for the system.web section to allow a 2G upload. Thanks for the tip, Yang Shen.

    <httpRuntime maxRequestLength = "2097151" />

    Thursday, July 25, 2019 12:08 AM