Jquery文件上传失败显示

时间:2013-08-21 16:12:59

标签: jquery json asp.net-mvc-4 jquery-file-upload multifile-uploader

您好我正在使用jquery文件上传显示失败我已尝试使用diffeerent数据类型但仍显示失败,我可以显示上传的图像但完成事件未触发,失败事件正在触发。

$('#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;
    }

});

控制器

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

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;
            }
        }

请尽快帮助我。

1 个答案:

答案 0 :(得分:0)

您的退货类型似乎有问题。试试这个:

    // 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;
        }
    }