<head id="Head1" runat="server">
<title>项目管理</title>
<script type="text/javascript" src="../../Scripts/uploadify/swfobject.js"></script>
<script type="text/javascript" src="../../Scripts/uploadify/jquery.uploadify.min.js"></script>
<link type="text/css" href="../../Scripts/uploadify/uploadify.css" rel="Stylesheet" />
<style type="text/css">
.tdDiv_3 a:hover
{
color:Red; cursor:pointer;
}
</style>
</head>
<table>
<tr>
<%--上传附件--%>
<td colspan="4">
<div style="width: 99%; margin-top: 10px;">
<span style="font-weight: bold; font-size: 14px;">附件上传</span><br />
<div style="width: 100%; line-height: 30px; height: 30px;">
<input type="file" name="uploadify" id="uploadify" />
<a href="javascript:update();" style="float: right;" data-options="iconCls:'icon-remove',plain :'true'"
class="easyui-linkbutton">上传</a> <a style="float: right;" href="javascript:$('#uploadify').uploadifyClearQueue()"
data-options="iconCls:'icon-remove',plain :'true'" class="easyui-linkbutton">取消上传</a>
<hr />
<div id="fileQueue">
</div>
</div>
</div>
<div style="width: 99%; margin-top: 10px;" id="fileList">
<span style="width: auto; font-weight: bold; font-size: 14px;">附件列表:</span><br />
<asp:Repeater ID="fileLit" runat="server">
<ItemTemplate>
<div class="tdDiv_3">
<a href="<%# Eval("filePath").ToString() %>" target="_blank"><%# (Container.ItemIndex + 1) +"、"+Eval("fileName").ToString()+"." + Eval("fileKzm").ToString()%></a><a οnclick="delefile('<%# Eval("filePath").ToString() %>',this)" style=" float:right;">删除</a></div>
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</table>
<--脚本-->
<script type="text/javascript">
var showInfo = "";
var num = 0;
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader': '../Scripts/uploadify/uploadify.swf',
'script': '../taizhang/Y/ashx/UploadFile.ashx',
'cancelImg': '../Scripts/uploadify/cancel.png',
'folder': 'upload',
'queueID': 'fileQueue',
'auto': false,
'multi': true,
onComplete: function (event, queueId, fileObj, response, data) {
if (response.indexOf("<br />") <= -1) {
$("#fileList").html($("#fileList").html() + "<div class=\"tdDiv_3\"><a href=\"" + response + "\">" + ($(".tdDiv_3").length + 1) + "、" + fileObj.name + "</a><a οnclick=\"delefile('" + response + "',this)\" style=\" float:right;\" >删除</a></div>");
} else {
showInfo += response; //返回内容
}
},
onAllComplete: function () {
if (showInfo != "") {
$.messager.alert("提示", showInfo);
}
}
});
});
//删除附件
function delefile(_path, _this) {
$.ajax({
type: 'POST',
url: "../taizhang/ashx1/fileDelete.ashx",
dataType: "html",
data: {
path: _path
},
success: function (data) {
if (data == "1") { $(_this).parent("div").remove(); }
else {
$.messager.alert("提示", data);
}
}
});
}
//更新
function update() {
showInfo = "";
num = 0;
var ssid = $("#row").val();
$('#uploadify').uploadifySettings('scriptData', { 'tbid': '1013', 'pid': ssid });
$('#uploadify').uploadifyUpload();
}
</script>
后台代码····
-----------删除-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Auxiliary;
using Auxiliary.DBUtility;
using System.IO;
namespace baohang.taizhang.ashx1
{
/// <summary>
/// fileDelete 的摘要说明
/// </summary>
public class fileDelete : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
string path = tools.getRequest("path");
string sql = string.Format("DELETE FROM [dbo].[t_file] WHERE filePath='{0}'", path);
DbHelperSQLP db = new DbHelperSQLP();
int i = db.ExecuteSql(sql);
if (i > 0)
{
context.Response.Write("1");
File.Delete(context.Server.MapPath(path));
}
else
{
context.Response.Write("删除失败!");
}
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
-------上传--------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Auxiliary.DBUtility;
using Auxiliary;
using System.IO;
using System.Web.SessionState;
namespace baohang.taizhang.ashx1
{
/// <summary>
/// UploadFile 的摘要说明
/// </summary>
public class UploadFile : IHttpHandler, IRequiresSessionState
{
DbHelperSQLP db = new DbHelperSQLP();
public void ProcessRequest(HttpContext context)
{
HttpPostedFile file = context.Request.Files["FileData"];
int tbid = tools.typeInt(tools.getRequest("tbid"));
string folder = context.Request["folder"]+"/";
string uploadpath = context.Server.MapPath(folder);
string userID = "";
int pid = tools.typeInt(tools.getRequest("pid"));
if (context.Session["UserID"] != null)
{
userID = context.Session["UserID"].ToString();
}
if (file != null)
{
if (!Directory.Exists(uploadpath))
{
Directory.CreateDirectory(uploadpath);
}
string FileName = file.FileName;
string _FileName = FileName.Substring(0, FileName.LastIndexOf("."));
string fileKzm = FileName.Substring(FileName.LastIndexOf(".")+1);
string guidName = System.Guid.NewGuid().ToString() + FileName.Substring(FileName.LastIndexOf(".")); //创建GUIP名称
//为方便管理,后期还要增加一个用户文件夹分类。
string SavePath = uploadpath + guidName; //保存路径
string _savePath = folder + guidName; //存在数据库内的虚拟路径,因为绝对路径是读取不了的。
DbHelperSQLP db = new DbHelperSQLP();
int i = db.ExecuteSql(string.Format(@"INSERT INTO [dbo].[t_file]([fileName],[filePath],[fileKzm],[fileSize],[tb_id],[fileUser],[pid])
VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}');", _FileName, _savePath, fileKzm, file.ContentLength, tbid, userID, pid));
//因为登录原因 先默认用户ID 0
if (i <= 0)
{
context.Response.Write("上传文件【" + FileName + "】错误!<br /><hr />");
context.Response.End();
}
file.SaveAs(SavePath);
context.Response.Write(_savePath);
}
else
{
context.Response.Write("上传文件为空!<br /><hr />");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}