Click here to Skip to main content
14,733,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to upload file selected using using ajax jquery at client side(homepage.html) and upload it to the server side (process.aspx.cs). can any one provide me the sample code?

What I have tried:

Client Side Code (homepage.html)
var file = $('#file')[0].files[0];
                type: "POST",
                url: "process.aspx/getFile",
                contentType: "application/json; charset=utf-8",
                data: "{'file': '" + Json.stringify(file) + "'}",
                dataType: "json",
                success: function (response) {
                failure: function (response) {

Server Side code (process.aspx.cs)
public static void getFile(string file) {
Updated 5-Dec-18 0:11am
Sinisa Hajnal 27-Feb-18 3:31am
Did you google it? I would expect you could find plenty of results. You should stringify entire data object, not just filename.
F-ES Sitecore 27-Feb-18 5:30am
This is extensively documented, please do basic research before asking a question, you can easily find the code to do this if you google.

As for finding the file path on the server use Server.MapPath to convert a path like "~/MyUploadFiles/mypic.jpg" to a physical path like "c:\inetnetpub\wwwroot\..."

Some minor issues in the code include the naming of the JSON helper, see here, JSON.stringify() - JavaScript | MDN[^] — notice the capitalization of JSON. Another thing that intrigues me, is, why are you stringifying the image? Did you intent to convert to base64? How to convert image into base64 string using javascript - Stack Overflow[^].

Since you wanted to do this using JavaScript (Ajax), there is a much simpler and easier way to handle this... Where you can submit files directly without having to mess up with the base 64. You need to check what enctype does your form is currently set to; it must be multipart/form-data. That is the only tricky part in this entire domain. Once that is done, you can even capture the file using, Request.Files collection.

I wrote a complete article with the sample code that you can use for this, please see: Uploading the Files – HTML5 and jQuery Way![^]
alpacaheng 27-Feb-18 5:23am
Hi, sir, I have reviewed your article, and I have a few question.
If not using razor, but using a normal webform, the HttpRequest object cannot be use.
The Server Side code (process.aspx.cs) is like this:
public static void saveFile()
int i = Request.File.Count;

another question is, how can i get the path of the file in server if i uploaded it to the folder before?
try using this code

if (Request.Files.Count > 0)
    HttpFileCollection files = Request.Files;
    for (int i = 0; i < files.Count; i++)
        HttpPostedFile postedFile = files[i];
        string savepath = "";
        string filename = Guid.NewGuid().ToString();
        postedFile.SaveAs(savepath + @"\" + filename);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900