使用Jquery上传文件.asp.net MVC 3



谁有一个清晰的建议实例化这个jquery的有用库?

我需要提交文件并管理来自服务器的Json响应。我总是在Js代码中得到none 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");
        }
    }

将Action的返回类型设置为ActionResult,并使用:

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

因此,在成功时,您将获得包含结果值的Json对象。

最新更新