0

I am using jquery-file-upload from blueimp (https://blueimp.github.io/jQuery-File-Upload/).

I have FileHandler.ashx for file uploads.

I am not able to return correct response from file handler. On javascript side, it always receives error. Please, guide me.

// File handler (FileHandler.ashx)

public void ProcessRequest(HttpContext context)
    {
        string fname = String.Empty;
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];                    
                fname = context.Server.MapPath("~/UserImageUploads/" + file.FileName);
                file.SaveAs(fname);
            }
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write(fname);
        context.Response.StatusCode = 200;
    }

// javascript part:

$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = 'FileHandler.ashx';
    $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {

        },
        fail: function (e, data) {

        },
    });
});

The resut is always "fail" and never "success". Error is SyntaxError: Unexpected token C. This is somehow related to Response I return, it is in incorrect format, but what format should it be there?

1 Answer 1

1

Try something like this

  public class ResponseMsg
    {
        public string status;
        public string name;
    }

public void ProcessRequest(HttpContext context)
    {
        string fname = String.Empty;
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile file = files[i];                    
                fname = context.Server.MapPath("~/UserImageUploads/" + file.FileName);
                file.SaveAs(fname);
            }
        }
        context.Response.ContentType = "text/plain";
       // context.Response.Write(fname);
      //  context.Response.StatusCode = 200;

        ResponseMsg r = new ResponseMsg
        {
            status = "success",
            name =fname
        };
       context.Response.Write(JsonConvert.SerializeObject(r));
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.