ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.[Solved] Uploading Multiple Files using ASP.Net FileUpload Control saves one File in Folder (Directory) in C#<blockquote><cite>chetan says:</cite> <div class="line number10 index9 alt1"><code class="csharp keyword">if</code> <code class="csharp plain">(rbyes.Checked)</code></div> <div class="line number11 index10 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number12 index11 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">if</code> <code class="csharp plain">(FileUpload1.HasFile)</code></div> <div class="line number13 index12 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number14 index13 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">string</code> <code class="csharp plain">FileName = System.IO.Path.GetFileName(FileUpload1.FileName);</code></div> <div class="line number15 index14 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">HttpFileCollection uploadedFiles = Request.Files;</code></div> <div class="line number16 index15 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text = </code><code class="csharp keyword">string</code><code class="csharp plain">.Empty;</code></div> <div class="line number17 index16 alt2">&nbsp;</div> <div class="line number18 index17 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">for</code> <code class="csharp plain">(</code><code class="csharp keyword">int</code> <code class="csharp plain">i = 0; i &lt; uploadedFiles.Count; i++)</code></div> <div class="line number19 index18 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number20 index19 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">HttpPostedFile userPostedFile = uploadedFiles[i];</code></div> <div class="line number21 index20 alt2">&nbsp;</div> <div class="line number22 index21 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">try</code></div> <div class="line number23 index22 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number24 index23 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">if</code> <code class="csharp plain">(userPostedFile.ContentLength &gt; 0)</code></div> <div class="line number25 index24 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number26 index25 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"&lt;u&gt;File #"</code> <code class="csharp plain">+ (i + 1) + </code><code class="csharp string">"&lt;/u&gt;&lt;br&gt;"</code><code class="csharp plain">;</code></div> <div class="line number27 index26 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"File Content Type: "</code> <code class="csharp plain">+ userPostedFile.ContentType + </code><code class="csharp string">"&lt;br&gt;"</code><code class="csharp plain">;</code></div> <div class="line number28 index27 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"File Size: "</code> <code class="csharp plain">+ userPostedFile.ContentLength + </code><code class="csharp string">"kb&lt;br&gt;"</code><code class="csharp plain">;</code></div> <div class="line number29 index28 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"File Name: "</code> <code class="csharp plain">+ userPostedFile.FileName + </code><code class="csharp string">"&lt;br&gt;"</code><code class="csharp plain">;</code></div> <div class="line number30 index29 alt1">&nbsp;</div> <div class="line number31 index30 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">userPostedFile.SaveAs(Server.MapPath(</code><code class="csharp string">"~/Agreements/"</code> <code class="csharp plain">+ FileName));</code></div> <div class="line number32 index31 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"Location where saved: "</code> <code class="csharp plain">+ FileName + </code><code class="csharp string">"~/Agreements/"</code> <code class="csharp plain">+ Path.GetFileName(userPostedFile.FileName) + </code><code class="csharp string">"&lt;p&gt;"</code><code class="csharp plain">;</code></div> <div class="line number33 index32 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number34 index33 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number35 index34 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">catch</code> <code class="csharp plain">(Exception Ex)</code></div> <div class="line number36 index35 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number37 index36 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">lblfileupmsg.Text += </code><code class="csharp string">"Error: &lt;br&gt;"</code> <code class="csharp plain">+ Ex.Message;</code></div> <div class="line number38 index37 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number39 index38 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number40 index39 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> </blockquote> <p>There is no problem with your code. You just need to declare the&nbsp;FileName inside the loop and get the file name from the&nbsp;HttpPostedFile.</p> <p>Refer the below modified code.</p> <pre class="brush: csharp">if (rbyes.Checked) { if (FileUpload1.HasFile) { HttpFileCollection uploadedFiles = Request.Files; lblfileupmsg.Text = string.Empty; for (int i = 0; i &lt; uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; string FileName = System.IO.Path.GetFileName(userPostedFile.FileName); try { if (userPostedFile.ContentLength &gt; 0) { lblfileupmsg.Text += &#34;&lt;u&gt;File #&#34; + (i + 1) + &#34;&lt;/u&gt;&lt;br&gt;&#34;; lblfileupmsg.Text += &#34;File Content Type: &#34; + userPostedFile.ContentType + &#34;&lt;br&gt;&#34;; lblfileupmsg.Text += &#34;File Size: &#34; + userPostedFile.ContentLength + &#34;kb&lt;br&gt;&#34;; lblfileupmsg.Text += &#34;File Name: &#34; + FileName + &#34;&lt;br&gt;&#34;; userPostedFile.SaveAs(Server.MapPath(&#34;~/Agreements/&#34; + FileName)); lblfileupmsg.Text += &#34;Location where saved: &#34; + Server.MapPath(&#34;~/Agreements/&#34; + FileName) + &#34;&lt;p&gt;&#34;; } } catch (Exception Ex) { lblfileupmsg.Text += &#34;Error: &lt;br&gt;&#34; + Ex.Message; } } }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/566315/Solved-Uploading-Multiple-Files-using-ASPNet-FileUpload-Control-saves-one-File-in-Folder-Directory-in-C/https://www.aspforums.net:443/Threads/566315/Solved-Uploading-Multiple-Files-using-ASPNet-FileUpload-Control-saves-one-File-in-Folder-Directory-in-C/Tue, 04 Dec 2018 00:50:07 GMT