用jquery uploadify上传插件上传文件

时间:2022-08-29 13:40:51
用jquery uploadify上传插件上传文件用jquery uploadify上传插件上传文件
 public void ProcessRequest(HttpContext context)
{
string esOIDs = System.Web.HttpContext.Current.Request["esOIDs"];
string userID=System.Web.HttpContext.Current.Session["USER_ID"].ToString();

context.Response.ContentType
= "text/plain";
context.Response.Charset
= "utf-8";

HttpPostedFile file
= context.Request.Files["Filedata"];
string Path = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\AccessoryFiles";

if (!Directory.Exists(Path))//判断文件是否存在
{
Directory.CreateDirectory(Path);
}

if (file != null)
{
if (file.ContentLength <= 1024 * 1024 * 1024)//限定文件小于100M
{
bool fileOK = false;
string fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower(); //得到选择文件 扩展名

string[] allowedExtensions = { ".doc", ".docx", ".ppt", ".pptx", ".xls", ".xlsx", ".pdf",".wmv",".avi", ".gif", ".png", ".bmp", ".jpg", ".mp4", ".rar", ".zip" }; //可上传文件类型列表
for (int i = 0; i < allowedExtensions.LongLength; i++)//判断上传文件是否为上述合法类型
{
if (fileExtension == allowedExtensions[i])
{
fileOK
= true;
break;
}
}

if (fileOK == true)//如果文件格式正确
{
string uploadPath = Path + "\\" + esOIDs;
if (!Directory.Exists(uploadPath))//判断文件是否存在
{
Directory.CreateDirectory(uploadPath);
}

string fileNewName = DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(file.FileName).ToLower();
file.SaveAs(uploadPath
+ "\\" + fileNewName);

if (!Directory.Exists(uploadPath + "\\" + fileNewName))
{
AccessoryObj accessoryObj
= new AccessoryObj();
accessoryObj.REFOID
= esOIDs;
accessoryObj.ReftypeVal
= AccessoryType.项目;
accessoryObj.FILENAME
= file.FileName;
accessoryObj.FILETYPE
= fileExtension;
accessoryObj.FILEPATH
= "FileUpLoad/AccessoryFiles/" + esOIDs + "/" + fileNewName;
accessoryObj.UPLOADUSER
= userID;
accessoryObj.UPLOADDATE
= DateTime.Now;

List
<AccessoryObj> accList = new List<AccessoryObj>();
accList.Add(accessoryObj);
new AccessoryRepository().AddAccessoryObj(accList);
}

//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
context.Response.Write("1");
}
}
}
else
{
context.Response.Write(
"0");
}
}

public bool IsReusable
{
get
{
return false;
}
}
uploadhandler。ashx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpLoad.aspx.cs" Inherits="FileUpLoad" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
//去网上download一个jquery uploadify插件 ,这些文件都有
<script src="../../Scripts/jquery/jquery.uploadify/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery/jquery.uploadify/jquery.uploadify.v2.1.0.min.js" type="text/javascript"></script> <script src="../../Scripts/jquery/jquery.uploadify/swfobject.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="../../Scripts/jquery/jquery.uploadify/uploadify.css" />

<script type="text/javascript">
$(document).ready(function () {
$(
"#uploadify").uploadify({
'uploader': '../../Scripts/jquery/jquery.uploadify/uploadify.swf',
'script': 'UploadHandler.ashx?esOIDs=<%=esOIDs %>',
'cancelImg': '../../Scripts/jquery/jquery.uploadify/cancel.png',
'folder': '../../FileUpLoad/',
'queueID': 'fileQueue',
'auto': false,
'multi': true,
'buttonText': '',
'fileDesc' :'可选择类型:.doc/.rar/.docx/.ppt/.pptx/.xls/.xlsx/.pdf/.gif/.png/.bmp/.jpg/.mp4/.rar/.wmv/.avi/.zip',
'fileExt': '*.doc;*.rar;*.docx;*.ppt;*.pptx;*.xls; *.xlsx;*.pdf;*.gif;*.png;*.bmp;*.jpg;*.mp4;*.rar;*.wmv;*.avi;*.zip', 'sizeLimit': 1024 * 1024 * 1024,
'buttonImg': '../../Scripts/jquery/jquery.uploadify/browse.png',
'height':'20',
'width': '50'
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="fileQueue" style="height: 400px"> </div>
<table>
<tr>
<td style="text-align: inherit">
<input type="file" name="uploadify" id="uploadify" onclick="return uploadify_onclick()" />
<input name="" onclick="javascript:$('#uploadify').uploadifyUpload()" type="button" style="height:20px" value="上传" />
<input name="" onclick="javascript:$('#uploadify').uploadifyClearQueue()" type="button" style="height:20px" value="取消上传" />
</td>
</tr>
</table>
</form>
</body> </html>