Click here to Skip to main content
11,492,291 members (64,886 online)
Click here to Skip to main content

Tagged as

Uploading and saving Files

, 14 Oct 2014 MIT 3.6K 3
Rate this:
Please Sign up or sign in to vote.
In the previous chapter, I showed some code and its properties of how it can be used to save the data from a form to a text (.txt) file. Now its time for you to understand how and why media files are different from the simple form.You need to encode the data of the media being sent to the server bef

In the previous chapter, I showed some code and its properties of how it can be used to save the data from a form to a text (.txt) file. Now its time for you to understand how and why media files are different from the simple form.

You need to encode the data of the media being sent to the server before it is sent to the server. For that you use an enctype and set a value to it inside the form element. Like the following the enctype attribute of the form is being evaluated to multipart/form-data.

<form method="post" enctype="multipart/form-data">

Now, when the form will be submitted, everything in it, will be first encoded to the relative type and then sent to server. Please note that the files (such as images, audio, video) need to be encoded. So that the server can process on them.

Now create the form in the page as

<form method="post" enctype="multipart/form-data">
  <input type="file" name="image" accept="image/*" />
  <input type="submit" value="Upload" />

Now when this form will be submitted the image will be submitted alongwith the data. Note the accept attribute, I have specified image in it, which will force the browser to only select the image (.jpg, .png etc) files not others such as audio (.mp3, .wav etc) and others. 

Now lets get to the server-side code:

if (IsPost) {
      int numberOfFiles = Request.Files.Count;
      for(int i =0; i < numberOfFiles; i++) {
          var uploadedFile = Request.Files[i];
          if (uploadedFile.ContentLength > 0) {
              fileName = Path.GetFileName(uploadedFile.FileName);
              fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +

A little elaboration

Now lets get the knowledge of the above code, and lets understand how it works.

You might be familiar with loops and variables upto now. This code makes use of that thing. You will be looping through the images that were selected, but note that this is required only when you are using multiple attribute for the form input element such as

<input type="file" name="image" accept="image/*" multiple />

Otherwise, you don’t require that.

Now, the first code that is a request to count the files that were selected. Then it counts there value and uses it as a condition inside a loop. Then after that, it goes back and keeps getting each file and saves it to the server using SaveAs. Note that in this code, we are using UploadedFile variable for each of the media file that is being sent. You can try to edit the code and see how it works. Good luck with it!


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Afzaal Ahmad Zeeshan
Pakistan Pakistan
Hi, my name is Afzaal Ahmad Zeeshan. I am a Pakistani, student, Ahmadi Muslim kid. I live in Rabwah and I am here since I was ~3years old. I have done my matriculation with major subjects for Science and Intermediate in Engineering.

I started learning programming when I was 15 and wrote small application that would get me double (not the data type) of the int value I would provide it with. In the beginning, I started Web applications, using ASP.NET when I was 16 and I really didn't know ASP.NET is written in C#, all I knew was that there is something called Razor syntax. I created simple web applications like Say hello to a friend, Add a friend in your list and get the elder among them. After craming the ASP.NET codes, I finally realized that there is no way to write application by cramming the API. So, I went learning, I learnt Visual C#, Java, Visual C++, started to learn Assembly but gave up in the middle, will restart this.

I have written some projects for my friends, I have made some web applications for cousins, I have developed a few applications for my cell phone, for desktop. I have developed an HTML keyboard for websites, like a On-Screen Keyboard, A Login system using simple C# code and JSON files. I am planning to write a Database system that would be much much much less costly than MS SQL. I am author at many blogs and teach programming to new developers to the extent that I have learnt programming.

I am never found complaining, I am self teaching, I am polite to elders, I accept my mistakes without criticizing the person who found a bug in me. I listen to Eminem, more than I listen to my girl. I am logical, you can make me quite my logically proving me wrong. Otherwise our dicussion might be a never ending loop.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

QuestionGreat Work Pin
eslipak21-Oct-14 14:30
professionaleslipak21-Oct-14 14:30 
AnswerRe: Great Work Pin
Afzaal Ahmad Zeeshan22-Oct-14 6:37
professionalAfzaal Ahmad Zeeshan22-Oct-14 6:37 
GeneralRe: Great Work Pin
eslipak22-Oct-14 12:19
professionaleslipak22-Oct-14 12:19 
GeneralRe: Great Work Pin
Afzaal Ahmad Zeeshan22-Oct-14 19:27
professionalAfzaal Ahmad Zeeshan22-Oct-14 19:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 14 Oct 2014
Article Copyright 2014 by Afzaal Ahmad Zeeshan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid