上传附件

   <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;
            }
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值