首页 技术 正文
技术 2022年11月19日
0 收藏 777 点赞 3,085 浏览 3345 个字

github连接地址:https://github.com/danialfarid/ng-file-upload

核心代码:

html:

<div class=”form-group”>

                <label>源文件:</label>

                <input class=”form-control h30 w356″  ng-model=”data.filePath”/>

                <select class=”form-control h30 w143″>

                    <option value=””>1</option>

                    <option value=””>2</option>

                </select>

                <div class=”button” ngf-select=”fileChanged($file)”>Upload on file select</div>

            </div>

js:

//var app = angular.module(‘main.app’, [‘bw.paging’, ‘cbc.datePicker’, ‘ngFileUpload’]);

//app.controller(‘main-controller’, function ($scope, $http, $log, $rootScope,
Upload) {

$scope.fileChanged = function (file) {

        Upload.upload({

            url: ‘/ImportSettlement/Upload’,

            data: { file: file }

        }).then(function (resp) {

            console.log(‘Success ‘ + resp.config.data.file.name + ‘uploaded. Response: ‘ + resp.data);

        }, function (resp) {

            console.log(‘Error status: ‘ + resp.status);

        });

    }

后台代码:

[HttpPost]

        public ActionResult Upload(HttpPostedFileBase file)

        {

            if (file == null)

            {

                return Content(“没有文件!”, “text/plain”);

            }

            var fileName = Path.Combine(Request.MapPath(“~/Upload”), Path.GetFileName(file.FileName));

            try

            {

                return Content(“上传异常 !”, “text/plain”);

                //file.SaveAs(fileName);

                ////tm.AttachmentPath = fileName;//得到全部model信息

                //tm.AttachmentPath = “../upload/” + Path.GetFileName(file.FileName);

                ////return Content(“上传成功!”, “text/plain”);

                //return RedirectToAction(“Show”, tm);

            }

            catch

            {

                return Content(“上传异常 !”, “text/plain”);

            }

        }

上述代码兼容IE10及以上浏览器,兼容IE9代码如下:

Html:

引入ng-upload-file组件

<script src=”/Scripts/angular/FileUpload/ng-file-upload.min.js”></script>

<script>

    //optional need to be loaded before angular-file-upload-shim(.min).js

    FileAPI = {

        debug: true,

//                forceLoad: true,

//                html5: false, //to debug flash in HTML5 browsers

        jsUrl: ‘/Scripts/angular/FileUpload/FileAPI.min.js’,

        flashUrl: ‘/Scripts/angular/FileUpload/FileAPI.flash.swf’,

    };

</script>

  <!–  for no html5 browsers support –>

<script src=”/Scripts/angular/FileUpload/ng-file-upload-shim.min.js”></script>

——————–上传部分

<div class=”button” ngf-select=”fileChanged($file)”>Upload on file select</div>

js执行

//var app = angular.module(‘main.app’, [‘bw.paging’, ‘cbc.datePicker’, ‘ngFileUpload’]);

//app.controller(‘main-controller’, function ($scope, $http, $log, $rootScope, Upload) {

$scope.fileChanged = function (file) {

        Upload.upload({

            url: ‘/ImportSettlement/Upload’,

            data: { file: file }

        }).then(function (resp) {

            console.log(‘Success ‘ + resp.config.data.file.name + ‘uploaded. Response: ‘ + resp.data);

        }, function (resp) {

            console.log(‘Error status: ‘ + resp.status);

        });

    }

后台接收:

[HttpPost]

        public ActionResult Upload(HttpPostedFileBase file)

        {

            if (file == null)

            {

                return Content(“没有文件!”, “text/plain”);

            }

            var fileName = Path.Combine(Request.MapPath(“~/Upload”), Path.GetFileName(file.FileName));

            try

            {

                return Content(“上传异常 !”, “text/plain”);

                //file.SaveAs(fileName);

                ////tm.AttachmentPath = fileName;//得到全部model信息

                //tm.AttachmentPath = “../upload/” + Path.GetFileName(file.FileName);

                ////return Content(“上传成功!”, “text/plain”);

                //return RedirectToAction(“Show”, tm);

            }

            catch

            {

                return Content(“上传异常 !”, “text/plain”);

            }

其他相关

http://www.cnblogs.com/zhouhb/p/3906714.html

http://www.cnblogs.com/zhangxiaolei521/p/5985790.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853