ASP.NET MVC 3 - 通过AngularJS文件上传 [英] ASP.NET MVC 3 - File upload through AngularJS

查看:105
本文介绍了ASP.NET MVC 3 - 通过AngularJS文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AngularJS 与我的网页,我有一个疑问:当初我张贴我的形式,我怎么能选择一些文件传递给我的ASP.NET MVC 3控制器?
检查了这一点:

I'm using AngularJS with my pages, and I have a doubt: when I do post with my form, how can I pass some selected file to my ASP.NET MVC 3 Controller? Check this out:

我的方式:

<form enctype="multipart/form-data" ng-controller="FilesController" ng-submit="submitingForm()">
    <div>
        Choose the file:
        <input type="file" onchange="angular.element(this).scope().setSelectedFile(this)" />
    </div>

    <input type="submit" value="Confirm" />
</form>

而AngularJS控制器:

And the AngularJS Controller:

var module = angular.module('application', []);

(function (ang, app) {

    function FilesController($scope, $http) {

        $scope.setSelectedFile = function (element) {
            $scope.$apply(function($scope) {
                $scope.selectedFile = element.files[0];
            });
        };

        $scope.submitingForm = function() {

            $http.post(url, ???????).success(function() {
                // How to pass that selected file for my ASP.NET controller?
            });
        }
    }

    app.controller("FilesController", FilesController);
})(angular, module);

感谢您!

推荐答案

我不太熟悉,AngularJS但如果您尝试使用一个AJAX请求上传文件,你可以忘掉它。这是不是可以做。您可以使用 HTML5文件API 如果你想异步文件上传到服务器。这里有一个<一个href=\"https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Example.3A_Uploading_a_user-selected_file\"相对=nofollow> 整节 致力于此。

I am not quite familiar with AngularJS but if you are attempting to upload a file using an AJAX request you can forget about it. That's not something that could be done. You could use the HTML5 File API if you want to upload files asynchronously to the server. There's an entire section dedicated to this.

如果您的客户端浏览器不支持HTML5文件API,你可以使用一个文件上传插件,如 FineUploader Uploadify (有相当多的人,只是谷歌)。

And if your client browsers do not support the HTML5 File API you could use a File Upload plugin such as FineUploader or Uploadify (there are quite many others, just google).

这篇关于ASP.NET MVC 3 - 通过AngularJS文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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