///
///
///
/// var swfu; //全局变量
//获取初始化设置项
function getSettings() {
return settings = {//此处所有的配置项在SWF说明文档中有,详见本项目中SWFUpload Document.html
upload_url: "FileUpload.ashx",
flash_url: "swfupload/swfupload.swf",
flash9_url: "swfupload/swfupload_fp9.swf",
file_post_name: "Filedata",
use_query_string: false,
requeue_on_error: false,
http_success: [201, 202],
assume_success_timeout: 0,
file_types: "*.*",
file_types_description: "所有文件",
file_size_limit: "10 GB",
file_upload_limit: 0,
file_queue_limit: 0,
debug: false,
//Added in v2.2.0 This boolean setting indicates whether a random value should be added to the Flash
//URL in an attempt to prevent the browser from caching the SWF movie. This works around a bug in some IE-engine based browsers.
//Note: The algorithm for adding the random number to the URL is dumb and cannot handle URLs that already have some parameters.
prevent_swf_caching: true,
//A boolean value that indicates whether SWFUpload should attempt to convert relative URLs used by the Flash Player to absolute URLs.
//If set to true SWFUpload will not modify any URLs. The default value is false.
preserve_relative_urls: false,
//按钮部分
button_placeholder_id: "btnUploadFile", //上传按钮所占用的区域
button_image_url: "img/ButtonNoText_75x21.png", //上传按钮图片效果
button_width: 77, //上传按钮宽
button_height: 21, //上传按钮高
button_text: "添加附件",
button_text_left_padding: 10,
button_text_top_padding: 1,
button_action: SWFUpload.BUTTON_ACTION.SELECT_FILES,
button_disabled: false,
button_cursor: SWFUpload.CURSOR.HAND,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
//以下是SWFUpload的事件,详见说明文档
swfupload_preload_handler: swfupload_preload_function,
swfupload_load_failed_handler: swfupload_load_failed_function,
file_queue_error_handler: file_queue_error_function,
file_dialog_complete_handler: file_dialog_complete_function,
upload_progress_handler: upload_progress_function,
upload_error_handler: upload_error_function,
upload_success_handler: upload_success_function,
upload_complete_handler: upload_complete_function,
swfupload_loaded_handler: swfupload_loaded_function,
mouse_click_handler: mouse_click_function,
mouse_over_handler: mouse_over_function,
mouse_out_handler: mouse_out_function,
file_dialog_start_handler: file_dialog_start_function,
file_queued_handler: file_queued_function,
upload_start_handler: upload_start_function,
debug_handler: debug_function
};
}
function InitSwf() {
if (swfu) {//初次需要销毁,不这么做会有BUG,忘了是什么,可以试试看
swfu.destroy();
}
swfu = new SWFUpload(getSettings()); //初始化SWFUpload对象
}
添加swfuploadevent.js
此处主要是一些上传动作对应的事件,如上传开始、失败、成功、完成等等都可以写在此处。
代码如下
复制代码
///
///
///
/// function swfupload_preload_function() {
if (!this.support.loading) {
Ext.MessageBox.alert("温馨提示", "您的Flash版本过低(支持9.028及更高版本), 请下载最新版Flash.");
//alert("You need the Flash Player 9.028 or above to use SWFUpload.");
return false;
}
}
//加载失败
function swfupload_load_failed_function() {
var t = this;
}
//加载后
function swfupload_loaded_function() {
var t = this;
}
function mouse_click_function() {
var t = this;
}
function mouse_over_function() {
var t = this;
}
function mouse_out_function() {
var t = this;
}
function file_dialog_start_function() {
var t = this;
}
function file_queued_function(file) {
var t = this;
}
//文件队列错
function file_queue_error_function(file, errorCode, message) {
var t = this;
}
//文件上传完成
function file_dialog_complete_function(fileSelectedCount, numberOfFilesQueued, totalNumberOfFilesInTheQueued) {
if (fileSelectedCount > 0) {
swfu.startUpload();//如果队列中还有未上传文件,则继续,若没有,一次只能上传一个文件
}
}
//开始上传
function upload_start_function(file) {
pbar.show();
btnCancelFileUpload.show();
}
//更新进度条
function upload_progress_function(file, completeBytes, totalBytes) {
var percent = Math.ceil((completeBytes / totalBytes) * 100);
pbar.updateProgress(percent / 100, file.name + ":" + percent + "%");
}
function upload_error_function(file, errorCode, message) {
var t = this;
}
//上传成功
function upload_success_function(file, data, received) {
if (received) {
var TopicRecord = Ext.data.Record.create([
{ name: 'ID', type: 'string' },
{ name: 'FileName', type: 'string' },
{ name: 'FileSize', type: 'int' },
{ name: 'FileExtension', type: 'string' },
{ name: 'FileCreationDate', type: 'date' },
{ name: 'FileModificationDate', type: 'date' },
{ name: 'FilePath', type: 'string' },
{ name: 'FileIndex', type: 'int' },
{ name: 'FileId', tyep: 'int' }
]);
var myNewRecord = new TopicRecord({
ID: file.id,
FileName: file.name,
FileSize: file.size,
FileExtension: file.type,
FileCreationDate: file.creationdate,
FileModificationDate: file.modificationdate,
FilePath: data,
FileIndex: file.index,
FileId: file.index
});
storeFiles.add(myNewRecord);//向Store中添加一行数据
}
}
//全部上传完成
function upload_complete_function(file) {
btnCancelFileUpload.hide();
pbar.reset();
pbar.updateText("");
pbar.hide();
swfu.startUpload();
}
function debug_function() {
var t = this;
}
function CancelFileUpload() {
var s = swfu;
swfu.cancelUpload();
}
添加Simple.js
此处主要是对界面的操作,与swfupload关系不大
代码如下
复制代码
///
///
///
///
//删除动作
function gpFlilesDelete(cmd, record, rowIndex) {
if (cmd === 'delete') {
storeFiles.remove(record);//移除record
}
}
//Store中移除了数据
function storeFilesRemoveEvent(store, record, rowIndex) {
var recordCount = store.getCount();
if (recordCount //调用后端Direct方法删除文件
Ext.net.DirectMethods.DeleteFile(record.data.FilePath, record.data.FileName);
}
//Store中添加了数据
function storeFilesAddEvent(store, record, rowIndex) {
gpFiles.show();
}
//换算文件大小
function renderSize(value, p, record) {
if (null == value || value == '') {
return "0 Bytes";
}
var unitArr = new Array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
var index = 0;
var srcsize = parseFloat(value);
var quotient = srcsize;
while (quotient > 1024) {
index += 1;
quotient = quotient / 1024;
}
return roundFun(quotient, 2) + " " + unitArr[index];
}
/*
四舍五入保留小数位数
numberRound 被处理的数
roundDigit 保留几位小数位
*/
function roundFun(numberRound, roundDigit) {
if (numberRound >= 0) {
var tempNumber = parseInt((numberRound * Math.pow(10, roundDigit) + 0.5)) / Math.pow(10, roundDigit);
return tempNumber;
} else {
numberRound1 = -numberRound
var tempNumber = parseInt((numberRound1 * Math.pow(10, roundDigit) + 0.5)) / Math.pow(10, roundDigit);
return -tempNumber;
}
}
using System;
using System.Web;
using System.IO;
namespace SwfUploadDemo
{
///
/// FileUpload 的摘要说明
///
public class FileUpload : IHttpHandler
{
public readonly string filePath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];
public void ProcessRequest(HttpContext context)
{
try
{
HttpPostedFile file;
for (int i = 0; i finally
{
context.Response.End();
}
}
}
}