简体   繁体   English

在ASP.NET MVC中发布序列化的ASP.NET模型和带有单个Jquery Ajax调用的文件

[英]Post a serialized ASP.NET Model and a file with single Jquery Ajax call in ASP.NET MVC

I am developing a movie library application in asp.net. 我正在asp.net中开发一个电影库应用程序。 In admin page, I need to add a movie to the database(details such as name, actors, poster url). 在管理页面中,我需要向数据库添加电影(详细信息,如姓名,演员,海报网址)。 And also I need to upload the trailer of the movie to a specific folder in the server. 而且我还需要将电影的预告片上传到服务器中的特定文件夹。

In my AddMovie View(which is strongly typed to a MovieDetails Model), I have a forms to include movie information as text and with a file input to upload a file(movie trailer). 在我的AddMovie视图(强类型为MovieDetails模型)中,我有一个表单,可以将电影信息包含为文本,并使用文件输入来上传文件(电影预告片)。 Within my controller I get the binded MovieDetails model and the uploaded file seperately as two parameters as follows. 在我的控制器中,我将绑定的MovieDetails模型和上传的文件分别作为两个参数,如下所示。

AddMovie Controller

At first I just posted from my view to this controller without using ajax. 起初我只是从我的视图发布到这个控制器而不使用ajax。 I was able to get the Model and the file to the controller to do what I needed to do. 我能够将模型和文件送到控制器来完成我需要做的事情。

Then I moved to ajax and tried to do this without page refresh. 然后我转移到ajax并尝试在没有页面刷新的情况下执行此操作。

在此输入图像描述

Now no files cannot be uploaded. 现在没有文件无法上传。 I tried to debug and noticed that uploadFile parameter of the controller is null although I attached a file to the file input in the view. 我尝试调试并注意到控制器的uploadFile参数为空虽然我将文件附加到视图中的文件输入。

Is there any way to post the model to the controller and upload the file without completely changing the controller and using a single ajax call? 有没有办法将模型发布到控制器并上传文件而无需完全更改控制器并使用单个ajax调用?

Or else is it possible pass the file path through a model attribute, access that file path and upload the relevant file in the controller, using Ajax. 或者是否可以通过模型属性传递文件路径,访问该文件路径并使用Ajax在控制器中上传相关文件。

Thank you! 谢谢!

As Darin mentioned, there is no simple way to handle file-uploading matters via ajax for some security reasons and you can't upload files to your server easily; 正如Darin所说,出于某些安全原因,没有简单的方法可以通过ajax处理文件上传问题,而且您无法轻松地将文件上传到服务器; but good news is there are some tricks which can resolve it, for instance, you can use jquery AjaxForm in your mvc form easily with a bit little modification. 但好消息是有一些技巧可以解决它,例如,您可以轻松地在mvc形式中使用jquery AjaxForm ,只需稍加修改即可。 Take a look at jquery-ajax-form and its samples, you will get some good idea around it. 看一下jquery-ajax-form及其样本,你会得到一些好主意。

Normally you cannot upload files using AJAX. 通常,您无法使用AJAX上传文件。 Modern browsers that support FormData would allow you to directly make AJAX requests containing multipart/form-data content. 支持FormData现代浏览器允许您直接生成包含multipart/form-data内容的AJAX请求。 Also look at the native XMLHttpRequest object. 另请查看本机XMLHttpRequest对象。

But if you need to support legacy browsers you will need some fallback mechanism such as hidden iframes or Flash movies. 但是,如果您需要支持旧版浏览器,则需要一些后备机制,例如隐藏的iframe或Flash电影。 Plugins such as blueimp would detect the browser capabilities and upload the file using the best option that the browser supports. blueimp这样的插件会检测浏览器功能并使用浏览器支持的最佳选项上传文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM