ASP.NET MVC 3 - 通过AngularJS文件上传文件上传、NET、ASP、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,...

我使用 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 如果你想异步文件上传到服务器。这里有一个 整节 致力于此。

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).

阅读全文

相关推荐

最新文章