angularjs 文件上传
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
angularjs 文件上传的更多相关文章
- angularjs 文件上传 如何绑定file表单的change?
ng-change 会失灵 , 唯一的解决方式 是 onchange="angular.element(this).scope().fileNameChanged()"然后利用 n ...
- webAPI+angularJS文件上传和下载
开发框架 前端 angularJS1.6 下载和保存文件FileSaver:https://github.com/eligrey/FileSaver.js/ 后端 .net WebAPI 1 导入Ex ...
- AngularJs 文件上传(实现Multipart/form-data 文件的上传)
<!-- 上传yml文件 --> <div class="blackBoard" ng-show="vm.showUpop==true"> ...
- [Angularjs]ng-file-upload上传文件
写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...
- AngularJs附件上传下载
首先:angular-file-upload 是一款轻量级的 AngularJS 文件上传工具,为不支持浏览器的 FileAPI polyfill 设计,使用 HTML5 直接进行文件上传. 第一步: ...
- angularjs结合plupload实现文件上传
转载注明:(罗志强的博客) angularjs的指令directive非常好使,可以很方便的结合各种插件,实现很强大的功能. 今天用到了plupload,就拿它举例吧. 正常的plupload用法应该 ...
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
写在前面 最近对文档库的知识点进行了整理,也就有了这篇文章,当时查找这些接口,并用在实践中,确实废了一些功夫,也为了让更多的人走更少的弯路. 系列文章 sharepoint环境安装过程中几点需要注意的 ...
- jquery.uploadify文件上传组件
1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
随机推荐
- Android开发失业50天,面了10家公司,唯二的offer也主动拒了
最近在论坛看到这样一个帖子: 坐标深圳. 4 月上旬公司解散.(现在想想好像是假解散,真裁员) 这一个半月以来,从朋友内推,到拉勾.Boss 直聘,再到猎聘.智联招聘. 从开始的精准投递,到后来的海投 ...
- React Class组件生命周期
一.react组件的两种定义方式 1.函数组件,简单的函数组件像下面这样,接收Props,渲染DOM,而不关注其他逻辑 function Welcome(props) { return <h1& ...
- rancher恢复kubecfg配置文件
docker run安装的单容器Rancher Server # 进入容器 docker exec -ti <容器ID> bash # 集群ID,可通过浏览器地址栏查询 cluster_i ...
- murmur3 hash(hash算法)
HashUtil.java package com.example.test.util; import com.google.common.base.Charsets; import com.goog ...
- Python - typing 模块 —— TypeVar 泛型
前言 typing 是在 python 3.5 才有的模块 前置学习 Python 类型提示:https://www.cnblogs.com/poloyy/p/15145380.html 常用类型提示 ...
- Python 可变数据类型与不可变数据类型
浅拷贝和深拷贝 Python数据都是存放到内存中的,Python的数据又分为可变和不可以变 可变数据(修改了值后,不会改变内存地址,修改的值还是指向相同的内存地址) 字典 # 列表是可变 x = [1 ...
- SQL 练习29
查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 SELECT Student.Sname,Course.Cname,SC.score FROM Student,Course,SC WHER ...
- QT从入门到入土(九)——TCP/IP网络通信(以及文件传输)
引言 TCP/IP通信(即SOCKET通信)是通过网线将服务器Server端和客户机Client端进行连接,在遵循ISO/OSI模型的四层层级构架的基础上通过TCP/IP协议建立的通讯.控制器可以设置 ...
- 【spring】spring 核心注解
注解具体分类如下: 1.模式注解 @Repository 数据仓储模式注解 @Component 通用组件模式注解 @Service ...
- WPF 中的 Command 命令
<Window x:Class="CommandDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx ...