找一个好的上传插件不容易啊,最近看好一个上传插件,查了些网上质料,自己做了些改动,记录下来,来彰显自己曾经屌丝过,这插件还不错,支持多个上传和预览
首先引用,发现有的时候想学点新的东西,不过时间久了也经不住时光的消磨啊,把但是研究好的立马记录下来,我想比自己想研究的时候在回想会有用深刻的多
<link href="~/css/plupload.css" rel="stylesheet" />@*plupload上传*@
<script src="~/Plupload/jquery.plupload.queue.min.js"></script>
<script src="~/Plupload/plupload.min.js"></script>
<script src="~/Plupload/plupload.flash.min.js"></script>
html代码
<div id='radioDigs'>
<div id="radioDig" title="选择你要上传的路径" style="width:auto;height:250px;padding:10px;">
<h5></h5>
</div>
</div>
<br/>
<div class="NewP" style="text-align:center;width:80%;">
<table style="text-align:center;width:100%;">
<tr>
<td><h5>您选择的上传路径为 : <span class='pathExplin'></span></h5></td>
<td><a href="#" class='newExplin' iconCls="icon-undo">重新选择上传路径</a></td>
</tr>
</table>
</div>
<br/>
<div id="flash_uploader" style="width: 850px; height: 500px;"> </div>
<div id="thumbnails"></div>
<script type="text/javascript">
var radioValue;//用于动态的获取单选框选中的值
function NewPlupload(){//重新选择上传路径
$('#radioDig').dialog({
modal:true,
onClose:function(){
radioValue = $('[name=ruploads]:checked').val();
PluploadShow();
}
});
}
$(function () {
$.ajax({
url:"/Home/Uploads",
//data:{},
type:"GET",
dataType:"text",
success:function(data,status){
$("#radioDig h5").html(data);//获取单项按钮
}
});
$('#radioDig').dialog({
modal:true,
buttons:[{
text:'保存',
iconCls:'icon-ok',
//left:50,
handler:function(){
radioValue = $('[name=ruploads]:checked').val();
PluploadShow();
$('#radioDig').dialog("close");
}
},{
text:'取消',
iconCls:'icon-no',
//left:50,
handler:function(){
$('#radioDig').dialog("close");
}
}]
});
$.messager.show({
title: "温馨提示",
msg: "上传图片尽量上传大小一致标准化的图片,以便修改!",
showType: 'show',
timeout: 5000
});
$(".NewP .newExplin").click(function(){
NewPlupload();
}).linkbutton({
plain:true
});
});
function PluploadShow(){//上传界面
$(".NewP .pathExplin").text(radioValue);
// 初始化Flash上传插件
$("#flash_uploader").pluploadQueue({
runtimes: 'flash', //使用Flash插件
url: '/Home/Uploadify/Upload', //服务器端响应页面
max_file_size: '10mb', //最大文件限制
file_size: '1mb', //一次上传数据大小
multipart_params: { "path": radioValue}, //传到后台的参数
unique_names: true, //是否自动生成唯一名称
filters: [ //文件类型限制
{ title: "图片文件", extensions: "jpg,gif,png,ico" },
{ title: "压缩文件", extensions: "zip,rar" }
],
//resize: { width: 320, height: 240, quality: 80 },// 压缩图片的大小
// SWF文件位置
flash_swf_url: '/Plupload/plupload.flash.swf',
init: {
FileUploaded: function (up, file, info) {
if (info.response != null) {
var jsonstr = eval("(" + info.response + ")");
for(var i = 0; i<jsonstr.length;i++){
var thumbnailUrl = jsonstr[i].thumbnailUrl;
var originalUrl = jsonstr[i].originalUrl;//上传完整路径
var name = jsonstr[i].name;//图片名
//一个文件上传成功
addImage(name, originalUrl, thumbnailUrl);
}
}
},
Error: function (up, args) {
//发生错误
if (args.file) {
alert('文件错误:' + args.file);
} else {
alert('出错' + args);
}
}
}
});
}
</script>
上传类(ServiceUpload)
/// <summary>
/// 取得缩略图。
/// </summary>
public void OutputThumbnail()
{
HttpContext context = HttpContext.Current;
//string imageName = context.Request.QueryString["ThumbnailId"] as string;
//if (imageName != null)
//{
List<ThumbnailInfo> thumbnails = (List<ThumbnailInfo>)context.Session["thumbnails"];
// List<ThumbnailInfo> thumbnails = context.Session["thumbnails"] as List<ThumbnailInfo>;
if (thumbnails != null)
{
foreach (ThumbnailInfo thumbnail in thumbnails)
{
//if (thumbnail.Id == imageName)
//{
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(thumbnail.Data);
context.Response.End();
return;
//}
}
} //}
context.Response.StatusCode = ;
context.Response.Write("没有创建");
context.Response.End();
} #endregion
/// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
public void UploadImage(string path)
{
HttpContext context = HttpContext.Current;
string uploadPath =context.Request.MapPath(path);//图片上传的路径
string thumbsImagePath = uploadPath;
// string uploadFileUrl = PathConfig.UploadUrl(); //上传文件的URL
HttpPostedFile uploadFile = context.Request.Files["file"];
try
{
//验证文件夹是否存在
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
} string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string imagePath = uploadPath + fileName + ".Jpeg";
Image originalImg = Image.FromStream(uploadFile.InputStream);
originalImg.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg); Image thumbnail = ImageProcess.MakeThumbnail(originalImg, , , ThumbnailMode.ByWidth);//略缩图
string thumbsPath = uploadPath + fileName + "-Thumbs.Jpeg";
thumbnail.Save(thumbsPath, System.Drawing.Imaging.ImageFormat.Jpeg); MemoryStream thumbsStream = ImageProcess.MakeThumbnail(originalImg, , );
ThumbnailInfo thumbnailInfo = new ThumbnailInfo(fileName, thumbsStream.GetBuffer());
thumbsStream.Close(); List<ThumbnailInfo> thumbnails = (List<ThumbnailInfo>)context.Session["thumbnails"];
if (thumbnails == null)
{
thumbnails = new List<ThumbnailInfo>();
context.Session["thumbnails"] = thumbnails;
}
thumbnails.Add(thumbnailInfo);
context.Session["thumbnails"] = thumbnails;
context.Session["path"] = thumbsPath;//自加属性
context.Response.StatusCode = ;
context.Response.Write("[{'name':'" + fileName + "','originalUrl':'" + PathConfig.GetVirtualPath(imagePath) + "','thumbnailUrl':'" + PathConfig.GetVirtualPath(thumbsPath) + "'}]"); }
catch
{
context.Response.StatusCode = ;
context.Response.Write("发生了一个错误");
context.Response.End();
}
}
略缩图类(ThumbnailInfo)
[Serializable]
public class ThumbnailInfo
{ #region Fields... #endregion #region Constructors... /// <summary>
/// 初始化 <see cref="ThumbnailInfo"/> 类的新实例。
/// </summary>
public ThumbnailInfo()
{ } /// <summary>
/// 初始化 <see cref="ThumbnailInfo"/> 类的新实例。
/// </summary>
public ThumbnailInfo(string id, byte[] data)
{
Id = id;
Data = data;
} #endregion #region Properties... /// <summary>
/// 获取或设置缩略图的编号。
/// </summary>
public string Id { get; set; } /// <summary>
/// 获取或设置缩略图的数据。
/// </summary>
public byte[] Data { get; set; }
}
}
后台代码,界面也是一个ui框架,自己写那是要死了,毕竟不是专业html的,此时功力还尚浅
public string Uploads()//上传按钮
{
List<Upload> list = oMan.GetUploads();
StringBuilder result = new StringBuilder();
string c = "checked='checked'";//默认第一个选中
foreach (Upload u in list)
{
result.Append("<input type='radio' name='ruploads' " + c + " value='" + u.UploadPath + "'/> " + u.UploadName + "<br/>");
if (c != "")
c = "";
}
return result.ToString();
}
public void Uploadify(string id)//上传处理
{
string path = "";
if(Request.Params["path"] != null)
path = Request.Params["path"].ToString();//上传路径
else if (Session["path"] != null)
path = Session["path"].ToString();//上传路径
string act = id;//上传资源类型
switch (act)
{
case "Upload":
new ServiceUpload().UploadImage(path);
break;
case "Thumbnail":
new ServiceUpload().OutputThumbnail();
break;
default:
new ServiceUpload().UploadImage(path);
break;
} }