0

Hi I am using jquery file upload showing fail i have tried with diffeerent datatype but still showing fail, i can show my image uploaded but done event is not firing, fail event is firing.

$('#frmsettings').fileupload({

    type: 'POST',
    dataType: 'application/json',

    url: '/Settings/UploadUiLogo',
    add: function (e, data)
    {
        data.submit();
    },
    progressall: function (e, data)
    {

    },
    done: function (e, data)
    {

        $.each(data.files, function (index, file)
        {
            alert("Done called");
        });
    },
    fail: function (e, data)
    {
        alert("Fail : Called");
        //window.location = JsErrorAction;
    }

});

Controller

======================================

public ContentResult UploadUiLogo()
        {
            try
            {
                if (Request.Files != null)
                {
                    foreach (string file in Request.Files)
                    {
                        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;

                        if (hpf.ContentLength == 0)
                            continue;
                        string savedFileName = Path.Combine(Server.MapPath("~/Content/uploadlogo"), Path.GetFileName(Guid.NewGuid() + hpf.FileName));
                        hpf.SaveAs(savedFileName);

                        return Content("{\"name\":\"" + savedFileName + "\"", "application/json");
                    }
                }

                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Please any one help me on it asap.

dotnetexpert
  • 145
  • 2
  • 16

1 Answers1

0

It seems like there may be a problem with your return type. Try this:

    // changed result type to JsonResult
    public JsonResult UploadUiLogo()
    {
        try
        {
            if (Request.Files != null)
            {
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;

                    if (hpf.ContentLength == 0)
                        continue;
                    string savedFileName = Path.Combine(Server.MapPath("~/Content/uploadlogo"), Path.GetFileName(Guid.NewGuid() + hpf.FileName));
                    hpf.SaveAs(savedFileName);

                    //create a c# object rather than writing the string
                    var result = new { name = savedFileName };
                    //json result return
                    return Json(result);
                }
            }

            return null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
anthr
  • 719
  • 5
  • 13