0

I am performing AJAX file upload operation in my ASP.net MVC application in which I also use angularjs.

Service method call:

    uploadFile: function() {
        var defer = $q.defer();
        var fileInput = document.getElementById("files");
        var messageHeaders = { 'Content-Type': 'application/x-www-form-urlencoded' };
        messageHeaders['X-File-Name'] = encodeURI(fileInput.files[0].name);
        messageHeaders['X-File-Type'] = encodeURI(fileInput.files[0].type);
        var fileData = fileInput.files[0];

        $http({
            url: window.ROOT + 'EmployerBenefit/UploadFile',
            method: "POST",
            data: fileData,
            headers: messageHeaders
        }).success(function(callback) {
            defer.resolve(callback);
        });
        return defer.promise;
    },

Controller Method:

[AllowAnonymous]
public JsonResult UploadFile(string attachmentType, int planId = 24527)
{
    var success = true;
    string message = ApplicationMessage.InvalidParameter;
    // Validation part removed
    if (success)
    {
        var inputStream = Request.InputStream;
        var fileLenght = (int) inputStream.Length;
        var fileName = HttpUtility.UrlDecode(Request.Headers["X-File-Name"]);
        var bytes = new byte[fileLenght];
        Request.InputStream.Read(bytes, 0, fileLenght);
        if (!Directory.Exists(Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId))))
        {
            Directory.CreateDirectory(Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId)));
        }
        System.IO.File.WriteAllBytes(
            Server.MapPath("/Content/Images/" + CommonFunctions.ParseString(planId) + "/" + fileName), bytes);

    }

    Response.StatusCode = (int)HttpStatusCode.OK;
    return Json(new {success, message}, JsonRequestBehavior.AllowGet);
}

Question:

I have an interceptor to handle response errors in a factory, even though I set the response status as OK the interceptor is picking up responseError (500)?

Where am I going wrong?

RandomUser
  • 1,843
  • 8
  • 33
  • 65

1 Answers1

0

I changed content type to undefined and this seems to work.

{ 'Content-Type': undefined };
RandomUser
  • 1,843
  • 8
  • 33
  • 65