Web方式的文件上传一直是个比较麻烦的问题,尤其是大尺寸(占用磁盘空间大)文件,幸亏有牛人引领Ajax潮流的到来,我们也有幸得以发挥。






<?xml version="1.0" encoding="utf-8"?>
configuration>
configSections>
section name="extnet" type="Ext.Net.GlobalConfig" requirePermission="false" />
configSections>
system.web>
httpRuntime executionTimeout="5400" maxRequestLength="20480000" useFullyQualifiedRedirectUrl="false" />
compilation debug="true" targetFramework="4.0" />
pages>
controls>
add assembly="Ext.Net" namespace="Ext.Net" tagPrefix="ext" />
controls>
pages>
httpHandlers>
add path="*/ext.axd" verb="*" type="Ext.Net.ResourceHandler" validate="false" />
httpHandlers>
httpModules>
add name="DirectRequestModule" type="Ext.Net.DirectRequestModule, Ext.Net" />
httpModules>
system.web>
appSettings>
add key="UploadPath" value="UpLoadFiles" />
appSettings>
extnet theme="Default" />
system.webServer>
validation validateIntegratedModeConfiguration="false" />
modules>
add name="DirectRequestModule" preCondition="managedHandler" type="Ext.Net.DirectRequestModule, Ext.Net" />
modules>
handlers>
add name="DirectRequestHandler" verb="*" path="*/ext.axd" preCondition="integratedMode" type="Ext.Net.ResourceHandler" />
handlers>
system.webServer>
configuration>
>
| 代码如下 | 复制代码 |
="false" >
Reader>
ext:JsonReader IDProperty="ID">
Fields>
ext:RecordField Name="ID" />
ext:RecordField Name="MailId" />
ext:RecordField Name="FilePath" />
ext:RecordField Name="FileName" />
ext:RecordField Name="FileSize" Type="Int" />
ext:RecordField Name="FileExtension" />
ext:RecordField Name="FileCreationDate" Type="Date" />
ext:RecordField Name="FileModificationDate" Type="Date" />
ext:RecordField Name="FileCreator" />
ext:RecordField Name="FileId" />
ext:RecordField Name="FileIndex" />
Fields>
ext:JsonReader>
Reader>
Listeners>
%>
="storeFilesRemoveEvent" />
%>
="storeFilesAddEvent" />
Listeners>
ext:Store>
|
|
| 代码如下 | 复制代码 |
="FitLayout">
Items>
ext:GridPanel runat="server" ID="gpFiles" StoreID="storeFiles" Title="已上传文件" Frame="true"
Layout="FitLayout" StripeRows="true" AutoExpandColumn="FileName">
ColumnModel>
Columns>
ext:Column ColumnID="FileName" DataIndex="FileName" Header="名称">
ext:Column>
ext:Column ColumnID="FilePath" DataIndex="FilePath" Header="FilePath" Hidden="true">
ext:Column>
ext:Column ColumnID="FileSize" DataIndex="FileSize" Header="大小">
Renderer Fn="renderSize" />
ext:Column>
ext:Column ColumnID="FileExtension" DataIndex="FileExtension" Header="类型">
ext:Column>
ext:Column ColumnID="FileCreationDate" DataIndex="FileCreationDate" Header="FileCreationDate"
Hidden="true">
ext:Column>
ext:Column ColumnID="FileModificationDate" DataIndex="FileModificationDate" Header="FileModificationDate"
Hidden="true">
ext:Column>
ext:Column ColumnID="FileCreator" DataIndex="FileCreator" Header="FileCreator" Hidden="true">
ext:Column>
ext:CommandColumn Width="24" Resizable="false">
Commands>
%>
="删除">
ext:GridCommand>
Commands>
ext:CommandColumn>
Columns>
ColumnModel>
SelectionModel>
ext:RowSelectionModel />
SelectionModel>
Listeners>
%>
="gpFlilesDelete" />
Listeners>
Buttons>
%>
="visibility:hidden;height:21;width:75">
ext:Button>
ext:Button runat="server" ID="btnCancelFileUpload" Text="取消" Hidden="true" >
Listeners>
Click Fn="CancelFileUpload" />
Listeners>
ext:Button>
Buttons>
BottomBar>
ext:StatusBar runat="server">
Content>
%>
="true">
ext:ProgressBar>
Content>
ext:StatusBar>
BottomBar>
ext:GridPanel>
Items>
ext:Viewport>
|
|
| 代码如下 | 复制代码 |
%>
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.**w*3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
html xmlns="http://www.w***3.org/1999/xhtml">
head runat="server">
title>SimpleSwfupload>
script type="text/javascript" src="swfupload/swfupload.js">>
script type="text/javascript" src="js/swfuploadevent.js">>
script type="text/javascript" src="js/InitSwfUpload.js">>
script type="text/javascript" src="Simple.js">>
style type="text/css">
.ContentsStyle
{
font-size: 14px;
}
>
head>
body>
form id="form1" runat="server">
ext:ResourceManager ID="ResourceManager1" runat="server">
Listeners>
%>
="return InitSwf();" />
Listeners>
ext:ResourceManager>
ext:Store ID="storeFiles" runat="server" ShowWarningOnFailure="false" >
Reader>
ext:JsonReader IDProperty="ID">
Fields>
ext:RecordField Name="ID" />
ext:RecordField Name="MailId" />
ext:RecordField Name="FilePath" />
ext:RecordField Name="FileName" />
ext:RecordField Name="FileSize" Type="Int" />
ext:RecordField Name="FileExtension" />
ext:RecordField Name="FileCreationDate" Type="Date" />
ext:RecordField Name="FileModificationDate" Type="Date" />
ext:RecordField Name="FileCreator" />
ext:RecordField Name="FileId" />
ext:RecordField Name="FileIndex" />
Fields>
ext:JsonReader>
Reader>
Listeners>
%>
="storeFilesRemoveEvent" />
%>
="storeFilesAddEvent" />
Listeners>
BaseParams>
ext:Parameter Name="id" Value="#{hiddId}.getRawValue()" Mode="Raw" />
BaseParams>
ext:Store>
ext:Viewport ID="Viewport1" runat="server" Layout="FitLayout">
Items>
ext:GridPanel runat="server" ID="gpFiles" StoreID="storeFiles" Title="已上传文件" Frame="true"
Layout="FitLayout" StripeRows="true" AutoExpandColumn="FileName">
ColumnModel>
Columns>
ext:Column ColumnID="FileName" DataIndex="FileName" Header="名称">
ext:Column>
ext:Column ColumnID="FilePath" DataIndex="FilePath" Header="FilePath" Hidden="true">
ext:Column>
ext:Column ColumnID="FileSize" DataIndex="FileSize" Header="大小">
Renderer Fn="renderSize" />
ext:Column>
ext:Column ColumnID="FileExtension" DataIndex="FileExtension" Header="类型">
ext:Column>
ext:Column ColumnID="FileCreationDate" DataIndex="FileCreationDate" Header="FileCreationDate"
Hidden="true">
ext:Column>
ext:Column ColumnID="FileModificationDate" DataIndex="FileModificationDate" Header="FileModificationDate"
Hidden="true">
ext:Column>
ext:Column ColumnID="FileCreator" DataIndex="FileCreator" Header="FileCreator" Hidden="true">
ext:Column>
ext:CommandColumn Width="24" Resizable="false">
Commands>
%>
="删除">
ext:GridCommand>
Commands>
ext:CommandColumn>
Columns>
ColumnModel>
SelectionModel>
ext:RowSelectionModel />
SelectionModel>
Listeners>
%>
="gpFlilesDelete" />
Listeners>
Buttons>
%>
="visibility:hidden;height:21;width:75">
ext:Button>
ext:Button runat="server" ID="btnCancelFileUpload" Text="取消" Hidden="true" >
Listeners>
Click Fn="CancelFileUpload" />
Listeners>
ext:Button>
Buttons>
BottomBar>
ext:StatusBar runat="server">
Content>
%>
="true">
ext:ProgressBar>
Content>
ext:StatusBar>
BottomBar>
ext:GridPanel>
Items>
ext:Viewport>
form>
body>
html>
|
|
| 代码如下 | 复制代码 |
/// /// /// /// 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对象 } |
|
| 代码如下 | 复制代码 |
/// /// /// /// function swfupload_preload_function() { if (!this.support.loading) { Ext.MessageBox.alert("温馨提示", "您的Flash版本过低(支持9.028及更高版本), |
|
| 代码如下 | 复制代码 |
/// /// /// /// //删除动作 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 Ext.Net;
namespace SwfUploadDemo
{
public partial class Simple : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
///
/// 删除文件
///
/// 实际存储名称
/// 原始名称
[DirectMethod]
public void DeleteFile(string fileName,string fileClientName)
{
try
{
System.IO.File.Delete(fileName);
X.Msg.Notify("提示", fileClientName + "已删除!").Show();
}
catch (Exception ex)
{
X.Msg.Notify("提示", fileClientName + "删除失败!").Show();
}
}
}
}
|
|
| 代码如下 | 复制代码 |
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();
}
}
}
}
|
|