62,072
社区成员
发帖
与我相关
我的任务
分享
@{
ViewBag.Title = "LibAddEdit2";
Layout = "~/Views/Shared/_Admin.cshtml";
}
@section Header{
@* <style type="text/css">@Url.Content("Scripts/jquery.plupload.queue/css/jquery.plupload.queue.css");</style>*@
<link href="/Scripts/Plupload/jquery.plupload.queue/css/jquery.plupload.queue.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<!-- Third party script for BrowserPlus runtime (Google Gears included in Gears runtime now) -->
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src="/Scripts/Plupload/plupload.full.js"></script>
<script type="text/javascript" src="/Scripts/Plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript" src="/Scripts/Plupload/i18n/zh-cn.js"></script>
<script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$(function () {
$("#uploader").pluploadQueue({
// General settings
runtimes: 'gears,flash,silverlight,browserplus,html5', // 这里是说用什么技术引擎
url: 'LibAddEdit', // 服务端上传路径
max_file_size: '10mb', // 文件上传最大限制。
chunk_size: '1mb', // 上传分块每块的大小,这个值小于服务器最大上传限制的值即可。
unique_names: true, // 上传的文件名是否唯一
// Resize images on clientside if we can
//// 是否生成缩略图(仅对图片文件有效)
resize: { width: 320, height: 240, quality: 90 },
// Specify what files to browse for
//// 这个数组是选择器,就是上传文件时限制的上传文件类型
filters: [
{ title: "Image files", extensions: "pdf" },
],
// Flash settings
// plupload.flash.swf 的所在路径
flash_swf_url: '/Scripts/Plupload/plupload.flash.swf',
// Silverlight settings
// silverlight所在路径
silverlight_xap_url: '/Scripts/Plupload/plupload.silverlight.xap'
});
// Client side form validation
// 这一块主要是防止在上传未结束前表带提交,具体大家可酌情修改编写
$('form').submit(function (e) {
var uploader = $('#uploader').pluploadQueue();
// Files in queue upload them first
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('StateChanged', function () {
if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
$('form')[0].submit();
}
});
uploader.start();
} else {
alert('You must queue at least one file.');
}
return false;
});
});
</script>
}
<h2>LibAddEdit2</h2>
<div id="uploader" >
<p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
</div>
/// <summary>
/// Handles chuncked file uploads like the ones from plupload.
/// </summary>
/// <param name="chunk"></param>
/// <param name="name"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Upload(int? chunk, string name)
{
var fileUpload = Request.Files[0];
var uploadPath = Server.MapPath("~/App_Data/Uploads");
chunk = chunk ?? 0;
//UPDATE 2/17/2011: Removed this since it doesn't work. I recommend setting the unique_names param client side if you want unique names.
////find a free filename if this is the first chunk
//if (!chunk.HasValue || chunk < 1)
//{
// int xx = 1;
// while (System.IO.File.Exists(uploadedFilePath))
// {
// uploadedFilePath = Path.Combine(uploadPath, Path.GetFileNameWithoutExtension(name) + "_" + xx + Path.GetExtension(name));
// xx++;
// }
//}
//TODO: cleanup old files
//write chunk to disk.
string uploadedFilePath = Path.Combine(uploadPath, name);
using (var fs = new FileStream(uploadedFilePath, chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
return Content("Success", "text/plain");
}