文件上传与阿贾克斯在asp.net mvc的 [英] File upload with ajax in asp.net mvc

查看:106
本文介绍了文件上传与阿贾克斯在asp.net mvc的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经讨论过很多次了。

I know this has been discussed a lot of times.

我基本上要在我看来,以更新文件的可能性。这个文件具有被映射到模型控制器期望:

I basically want the possibility in my view to update a file. This file has to be mapped to the model the controller expects:

public ActionResult Create(Company company)
{
    //Do something with the received model
}

型号:

public class Company
{
    public int Id { get; set; }
    public HttpPostedFileBase PictureUpload { get; set; }
    ...
}

这是工作没有任何问题。 现在我想通过AJAX送我的表单数据,包括文件,。 所以我在我看来,用这个:

This is working without any problems. Now I'd like to send my form data, including the file, via AJAX. Therefore I'm using this in my view:

@using (Ajax.BeginForm("Create", "Company", null, new AjaxOptions { HttpMethod = "Post", OnSuccess = "ajaxOnSuccess", OnFailure = "alert('Error message.');" }, new { @class = "ym-form", enctype = "multipart/form-data" }))

这是主要工作,但该文件上传不工作(据我读到阿贾克斯没有获得,因此它不能发送文件)。

This is basically working but the file upload doesn't work (as far as I read ajax doesn't have access to the file so it can't be sent).

我想什么是对这个问题的最佳解决方案,而无需修改我的后端(控制器/型号)。

I'd like what's the best solution for this problem without having to modify my backend (controller/model).

电子。 G。我读了这篇文章: http://ajeeshms.in/Blog/Article / 1 /上载文件 - 使用Ajax的功能于ASP-MVC

E. g. I read this article: http://ajeeshms.in/Blog/Article/1/upload-files-using-ajax-in-asp-mvc

它提供了两个漂亮的可能性,但我不得不修改后端,因为就我看到的自动映射到在我的模型的HttpPostedFileBase类型是不可能的了。

It provides two nice possibilities but I'd have to modify the backend because as far as I see the automatically mapping to the HttpPostedFileBase type in my model wouldn't be possible anymore.

我不介意使用任何插件的工作对我的看法,或者使用支持的唯一的新浏览器的技术。

I don't mind using any working plugin for my view or using a technique which is supported by new browsers only.

推荐答案

试试这个code

//添加引用form.js

//Add reference to form.js

<script src="http://malsup.github.com/jquery.form.js"></script>

@using (Html.BeginForm("Create", "Company", FormMethod.Post, new { @enctype ="multipart/form-data",@id="formid" }))
{

}

//Javascript code

<script type="text/javascript">

$('#formid').ajaxForm(function (data) {

});

</script>

这将作为阿贾克斯提交。

This will work as ajax submit.

//你可以得到当作ajaxForm 这里详情

//You can get more details on AjaxForm here

这篇关于文件上传与阿贾克斯在asp.net mvc的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆