3

有没有人有一个干净的建议来实例化这个 jquery 的有用库。

我需要提交文件并管理来自服务器的Json响应。我总是在Js代码中没有得到任何 json 响应。我已经查看了一些提到它的文章,但代码不符合目的。情况是:我实现了提交并保存在数据库中,但 Json 响应从未到达。

提前致谢。

这是我的视图代码:

<script type="text/javascript">

$("#formUplImg").fileupload({
    dataType: "json",
    url:'@Url.Action("CreateJson","ProductImage")',
    done: function (e, data) {
        alert(data.StatusMessage);
    }
});

</script>



@using (Html.BeginForm("CreateJson", "ProductImage", FormMethod.Post, new { id = "formUplImg", enctype = "multipart/form-data", @class = "jqtransform" }))
{
@Html.ValidationSummary(true)

<div class="rowElem">
    <input type="file" id="Content" name="Content" />
</div>
<div class="rowElem">
    @Html.ValidationMessageFor(item => item.Content)
</div>

<div class="rowElem">@Html.JQueryUI().Button("Guardar imagen", ButtonElement.Button, ButtonType.Submit, new { id = "guardar_imagen" })</div>
}

这是我的控制器操作代码:

  [HttpPost]
    public ContentResult CreateJson(UploadedFileInfo fileInfo)
    {
        try
        {
            if (fileInfo.Content == null)
                throw new Exception("Hubo problemas con el envío. Seleccione un archivo a subir");

            var file = new TempDocument
            {
                CreatedBy = User.Identity.Name,
                CreationTime = DateTime.Now,
                FileName = fileInfo.Content.FileName,
                MimeType = fileInfo.Content.ContentType,
                Size = fileInfo.Content.ContentLength,
                Content = new byte[fileInfo.Content.ContentLength]//Image content to save
            };

            fileInfo.Content.InputStream.Read(file.Content, 0, fileInfo.Content.ContentLength);//Reading image content into ProductImage object
            DocumentsManager.StorePendingDocuments.Add(file);
            DocumentsManager.SaveTempDocuments();//Store each document uploaded to: TempDocument Table

            TempData["SuccessMsg"] = "The image was saved successfully";

            var json = new JavaScriptSerializer().Serialize(new { Success = true, StatusMessage = "El objeto fue insertado correctamente" });
            return Content(json, "application/json");
        }
        catch (Exception exception)
        {
            TempData["ErrorMsg"] = exception.Message;
            var json = new JavaScriptSerializer().Serialize(new { Success = false, StatusMessage = exception.Message });
            return Content(json, "application/json");
        }
    }
4

1 回答 1

-1

使用 Action 的返回类型作为 ActionResult 并使用:

`return Json(new { Result = "Success" });`

这样一来,您将获得包含结果值的 Json 对象。

于 2013-06-14T11:03:27.780 回答