下载控件:uploadify 分为2种,选择Flash Version版本
地址:http://www.uploadify.com/
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="Hot.Upload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="Scripts/upload/uploadify.css" rel="stylesheet" />
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/upload/jquery.uploadify.min.js"></script>
<title>上传控件</title>
<script type="text/javascript">
$(document).ready(function () {
$("#uploadify").uploadify({
'swf': 'Scripts/upload/uploadify.swf',
'uploader': '/UploadHandler.ashx',
'cancelImg': 'Scripts/upload/uploadify-cancel.png',
'queueID': 'fileQueue',
'uploadLimit': 1000, //一次浏览器课上成总数量
//'fileSizeLimit': '100MB', //单个文件大小设置
'auto': false,
'multi': true,
'onFallback':function(){
alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
},
//返回一个错误,选择文件的时候触发
'onSelectError': function (file, errorCode, errorMsg) {
switch (errorCode) {
case -100:
alert("上传的文件数量已经超出系统限制的" + $('#file_upload').uploadify('settings', 'queueSizeLimit') + "个文件!");
break;
case -110:
alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#file_upload').uploadify('settings', 'fileSizeLimit') + "大小!");
break;
case -120:
alert("文件 [" + file.name + "] 大小异常!");
break;
case -130:
alert("文件 [" + file.name + "] 类型不正确!");
break;
}
alert(errorCode);
},
'onUploadStart': function (file) {
},
'onUploadSuccess': function (file, data, response) {
$('#' + file.id).find('.data').html(' 上传完毕');
},
'onUploadComplete': function () {
}
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<a href="javascript:$('#uploadify').uploadify('upload','*')">上传</a>|
<a href="javascript:$('#uploadify').uploadify('cancel', '*')">取消上传</a>
</p>
</body>
</html>
上传到后台:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace Hot
{
/// <summary>
/// UploadHandler 的摘要说明
/// </summary>
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//接收上传后的文件
HttpPostedFile file = context.Request.Files["Filedata"];
//其他参数
//string somekey = context.Request["someKey"];
//string other = context.Request["someOtherKey"];
//获取文件的保存路径 创建uploads文件夹
string uploadPath =HttpContext.Current.Server.MapPath("uploads" + "\\");
//判断上传的文件是否为空
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
//1.利用InputStream 属性直接从HttpPostedFile对象读取文本内容
System.IO.Stream MyStream;
int FileLen;
FileLen = file.ContentLength;
// 读取文件的 byte[]
byte[] bytes = new byte[FileLen];
MyStream = file.InputStream;
MyStream.Read(bytes, 0, FileLen);
//2.保存到文件
file.SaveAs(uploadPath + file.FileName);
context.Response.Write("1");
}
else
{
context.Response.Write("0");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
1.记得创建uploads文件夹,放上传的文件。
2.$('#' + file.id).find('.data').html(' 上传完毕'); 是设置显示为中文完成,不然是英文,也可以更改jquery.uploadify.js文件里的英文字。
标签:控件,file,文件,uploadify,System,context,上传 From: https://blog.51cto.com/u_15898776/5902201