Alex
Alex

Reputation: 3968

Upload a file to a server using ASP.NET and C#

I am new to forms and am trying to get an understanding of what is going on. I have looked at lots of questions and tutorials, but feel unclear on certain points.

So far I have created the following form in an aspx page:

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="#">
    <span class="txtSmallGrey fl" style="display:block; width:200px; margin:15px; margin-bottom:2px">
        <%= oUtils.GetContentText("Collect_Config_upload_sound") %>
    </span>
    <input type="file"  name="SoundFile" id="SoundFile" style="margin:15px; margin-bottom:2px">        
    <input type="submit" value="Upload" id="submit" style="float:left; margin-left:245px; margin-top:1px; height:20px;">    
</form>

and I have the following script at the top of the page:

<%
        if(Request.Form["SoundFile"] != "")
        {
            HttpPostedFile file = Request.Files["SoundFile"];
            string fname = Path.GetFileName(file.FileName);
            file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
        }
%>

I have a reasonable understanding of AJAX so some of this seems familiar to me.

Now to explain what I understand:

The form is declared and given the id of 'uploadbanner'. As I am transferring a file I have to include 'enctype...' I am posting as it is more secure and more flexible.

The action term tells the form where to post to. In this case I have put the C# code at the top of the page so do not need to include an asp.net page address to process this. If I did, I would include an asp.net page, in the same way as I would for AJAX (I think?).

Anything with an input tag inside the form tags will be posted in the form, and will send the name and value.

When the submit button is pressed, the form will be submitted to the the server side code for processing. So far I feel I understand what is going on.

Now the parts I feel less clear about,

Upvotes: 0

Views: 3309

Answers (1)

Tez Wingfield
Tez Wingfield

Reputation: 2251

Is it the case the when the 'submit' button is pressed, the C# code at the top of the page will be activated, and so if the field was blank it would not do anything?

Yes, Every time your page loads it will run the C# code, One would assume that if you submit the form it will check the posted form data. It's probably best to check HTTP headers.

And if the button was pressed multiple times, would the form be submitted multiple times?

Yes, the form will be submitted multiple times.

If so, then this is much the same way as AJAX?, and the file will simply be passed to the C# code, from where I can do what I need with it?

It's similar in the sense of HTTP requests but your posting to the page with a file attached and then C# checks with the page has file attached by running the Request.Form and then Request.Files and Posts to the server.

My final questions is, can I submit the form using an alternate method to the submit button, eg can I make a normal JavaScript button and tell it to submit the form?

What do you mean by a normal JavaScript button? You don't have to use a submit button. As long as your passing in a file and the page is loaded, the code will still run. I have approach where as i post to a HTTPHandler. Code snippet below:

public void ProcessRequest(HttpContext context)
 {
    context.Response.ContentType = "text/plain";
    HttpPostedFile postedFile = context.Request.Files["Filedata"];
    string filename = postedFile.FileName;
    var Extension = filename.Substring(filename.LastIndexOf('.') 
    + 1).ToLower();

    string savepath =HttpContext.Current.Server.MapPath("/images/profile/");

    postedFile.SaveAs(savepath + @"\" + user.uid + filename);
 }

So when submitting i point to the HttpHandler.ashx which is a class that implements the IHttpHandler interface, Which in turn gets the current context.

Upvotes: 1

Related Questions