【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用
参考:http://www.cnblogs.com/luotaoyeah/p/3321070.html
1、下载 uploadify 组件,copy至 Content文件夹
<link href="~/Content/uploadify/uploadify.css" rel="stylesheet" />
<script src="~/Content/uploadify/jquery.uploadify.js"></script>
2、View
<script type="text/javascript">
$(function () {
$('#btn_upload').uploadify({
uploader: '/home/upload', // 服务器处理地址
swf: '/Content/uploadify/uploadify.swf',
buttonText: "上传文件", //按钮文字
height: , //按钮高度
width: , //按钮宽度
fileTypeExts: "*.jpg;*.png;", //允许的文件类型
fileTypeDesc: "请选择图片文件", //文件说明
formData: { "imgType": "normal" }, //提交给服务器端的参数
onUploadSuccess: function (file, data, response) { //一个文件上传成功后的响应事件处理
var data = $.parseJSON(data);
$("#photo").attr("src",data.imgpath);
//alert(data.imgpath);
}
});
}); </script> <span id="btn_upload"></span>
<br />
<img id="photo" src="http://www.yxweb.com.cn/images/upphoto.gif" alt="请上传工作照" />
3、Controller
public ActionResult Upload(HttpPostedFileBase Filedata)
{
// 没有文件上传,直接返回
if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == )
{
return HttpNotFound();
} //获取文件完整文件名(包含绝对路径)
//文件存放路径格式:/files/upload/{日期}/{md5}.{后缀名}
//例如:/files/upload/20130913/43CA215D947F8C1F1DDFCED383C4D706.jpg
string fileMD5 = GetStreamMD5(Filedata.InputStream);
string FileEextension = Path.GetExtension(Filedata.FileName);
string uploadDate = DateTime.Now.ToString("yyyyMMdd"); string imgType = Request["imgType"];
string virtualPath = "/";
if (imgType == "normal")
{
virtualPath = string.Format("~/files/upload/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
}
else
{
virtualPath = string.Format("~/files/upload2/{0}/{1}{2}", uploadDate, fileMD5, FileEextension);
}
string fullFileName = this.Server.MapPath(virtualPath); //创建文件夹,保存文件
string path = Path.GetDirectoryName(fullFileName);
Directory.CreateDirectory(path);
if (!System.IO.File.Exists(fullFileName))
{
Filedata.SaveAs(fullFileName);
} var data = new { imgtype = imgType, imgpath = virtualPath.Remove(, ) };
return Json(data, JsonRequestBehavior.AllowGet);
} /// <summary>
/// 计算文件流的md5值
/// </summary>
/// <param name="stream">文件输入流</param>
/// <returns></returns>
public static String GetStreamMD5(Stream stream)
{
MD5 md5Hasher = new MD5CryptoServiceProvider();
/*计算指定Stream对象的哈希值*/
byte[] arrbytHashValue = md5Hasher.ComputeHash(stream);
/*由以连字符分隔的十六进制对构成的String,其中每一对表示value中对应的元素;例如“F-2C-4A”*/
string strHashData = System.BitConverter.ToString(arrbytHashValue).Replace("-", "");
return strHashData;
}
【要什么自行车】ASP.NET MVC4笔记02:上传文件 uploadify 组件使用的更多相关文章
- asp.net中FileUpload得到上传文件的完整路径
asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...
- asp dotnet core 支持客户端上传文件
本文告诉大家如何在 asp dotnet core 支持客户端上传文件 新建一个 asp dotnet core 程序,创建一个新的类,用于给客户端上传文件的信息 public class Kanaj ...
- ASP代码审计学习笔记 -3.上传漏洞
1.ASP上传过程抓包分析: POST /4.asp HTTP/1.1 Host: 192.168.1.102 User-Agent: Mozilla/5.0 (Windows NT 10.0; WO ...
- ASP.NET MVC 4 批量上传文件
上传文件的经典写法: <form id="uploadform" action="/Home/UploadFile" method="post& ...
- ASP.NET MVC 4 Ajax上传文件
这两天一直纠结着表单的问题.想在一个表单里实现三个功能: 输入查询条件,点击查询: 导出查询数据: 上传文件: 方法有很多,乱花渐欲迷人眼,尝试了很多,无果.大致说的是,给不同按钮写js代码,在js代 ...
- ASP.NET MVC上传文件----uploadify的使用
课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...
- asp.net 一般处理程序接收上传文件的问题
在使用Html+ashx处理文件上传时,遇到上传文件超过4M的问题,首先HTML代码如下: <!DOCTYPE html> <html> <head> <me ...
- ASP.NET Core MVC如何上传文件及处理大文件上传
用文件模型绑定接口:IFormFile (小文件上传) 当你使用IFormFile接口来上传文件的时候,一定要注意,IFormFile会将一个Http请求中的所有文件都读取到服务器内存后,才会触发AS ...
- asp.net 跨域服务器 上传文件
最近一个项目遇到问题,公司局域网中,在F域下有个服务器Fa,我们的项目就部署在Fa.项目里面有个功能,需要把生成的文件(.doc,.xls,.pdf)保存到O域的一台文件共享服务器Ob.在原有的网络环 ...
随机推荐
- Java提高篇(三二)-----List总结
前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList.LinkedList.Vector.Stack,通过这几个知识点可以对List接口有了比较深的了解了.只有通过归纳总结的知识 ...
- the bundle at bundle path is not signed using an apple submission certificate
在app上架的时候,出现这个错误,也许只是你的Apple Worldwide Developer Relations Certification Authority Intermediate Cert ...
- AngularJS快速入门指南16:Bootstrap
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- .net版本发展历史
最近装上了VS2013,发现好多新特性.新功能,公司办公还在使用VS2005.VS2008,不过用着也很顺手,在最新版Visual Studio中,微软加入了git源码管理工具,和之前的TFS大体上类 ...
- Repeater绑定数组并显示其值
web开发中,尤其是对于数据展示,不得不说Repeater是一个万能的控件,而且使用也很方便. 在ASP.NET中将数组绑定到Repeater中请问如何在Repeater前台页面中显示该数组的值? s ...
- oracle 表类型变量的使用
转载于:http://www.itxuexiwang.com/a/shujukujishu/oracle/2016/0216/89.html?1455872314 使用记录类型变量只能保存一行数据,这 ...
- Java线程:线程状态的转换
Java线程:线程状态的转换 一.线程状态 线程的状态转换是线程控制的基础.线程状态总的可分为五大状态:分别是生.死.可运行.运行.等待/阻塞.用一个图来描述如下: 1.新状态:线程对象已 ...
- 理解与模拟一个简单web服务器
先简单说下几个概念,根据自己的理解,不正确请见谅. web服务器 首先要知道什么是web服务器,简单说web服务器就是可以使用HTTP传输协议与客户端进行通信的服务器.最初的web服务器只能用来处理静 ...
- iOS开发中 workspace 与 static lib 工程的联合使用
在iOS开发中,其实workspace的使用没有完全发挥出来,最近做了一些研究,也想把之前写过的代码整理下,因为iOS里面的布局方式,交互方式也就那么几种.所以,整理好了之后,更能快捷开发,而且能够形 ...
- IllegalStateException : Web app root system property already set to different value问题详解
一.问题描述 最近公司有了一个新项目,这个项目最近部署到测试服务器上的时候出现了一个问题. 严重: Exception sending context initialized event to ...