0

嗨,我正在使用 jquery 文件上传显示失败我已经尝试过使用不同的数据类型但仍然显示失败,我可以显示我的图像上传但完成事件没有触发,失败事件正在触发。

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

请任何人尽快帮助我。

4

1 回答 1

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;
        }
    }
于 2013-08-28T15:45:54.543 回答