本帖最后由 gwf25sz 于 2011-04-08 11:43:58 编辑

解决方案 »

  1.   


    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Telerik.WebControls;
    using System.IO;namespace Honeywell
    {
        public partial class FileList : System.Web.UI.Page
        {
            private HttpCookie cookie = null;
            private string userAccount = "";
            private string sql = "";
            private string sqlPath = "\\Uploads\\";
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            
            /// <summary>
            /// Bind datalist
            /// </summary>
            private void BindData()
            {
                if (Request.QueryString["prnum"] != null && Request.QueryString["prnum"].Trim() != "")
                {
                    hfPRNumber.Value = Request.QueryString["prnum"].Trim();
                    sql = "SELECT *,(SELECT EnglishName FROM PR_Users WHERE USERID=UploadBy) as EName FROM PR_Attachment WHERE PRNumber = '" + hfPRNumber.Value.Trim() + "'";
                    DataTable dt = DBBase.GetDataTable(sql);
                    dlList.DataSource = dt;
                    dlList.DataBind();
                }
                if (Request.QueryString["status"] != null)
                {
                    dvUp.Style.Add("display", "none;");
                }
            }        protected void dlList_ItemDataBound(object sender, DataListItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    HiddenField hfUpBy = (HiddenField)e.Item.FindControl("hfUpBy");                cookie = Request.Cookies["adUser"];
                    userAccount = cookie == null ? "test" : cookie["account"].Trim();
                    LinkButton lbtn = (LinkButton)e.Item.FindControl("lbtnDelete");
                    lbtn.Visible = hfUpBy.Value.Trim() == userAccount ? true : false;
                    lbtn.Visible = Request.QueryString["status"] != null ? false : true;
                }
            }        protected void dlList_ItemCommand(object source, DataListCommandEventArgs e)
            {
                if (e.CommandName == "delete")
                {
                    sql = "DELETE PR_Attachment WHERE ID = '" + dlList.DataKeys[e.Item.ItemIndex].ToString() + "'";
                    DBBase.Update(sql);
                    BindData();
                    HiddenField hf = (HiddenField)e.Item.FindControl("hfAttachment");
                    File.Delete(Server.MapPath(hf.Value));
                    WebPage.ShowMessage("Delete success!");
                }
            }        protected void btnSave_Click(object sender, EventArgs e)
            {
                if(ffFile.Text == "")
                {
                    WebPage.ShowMessage("Please select the file.");
                    return;
                }
                if (txtDescription.Text == "")
                {
                    WebPage.ShowMessage("Please add descriptions to the file you have uploaded.");
                    return;
                }            string Path = Server.MapPath(@"~/Uploads/");            //如果路径不存在,则创建
                if (System.IO.Directory.Exists(Path) == false)
                {
                    System.IO.Directory.CreateDirectory(Path);
                }
                
                //组合路径,file.GetName()取得文件名
                Path = Path + fupload.FileName;
                sqlPath += fupload.FileName;
                //保存
                fupload.SaveAs(Path);            cookie = Request.Cookies["adUser"];
                userAccount = cookie == null ? "test" : cookie["account"].Trim();
                string pNo = Request.QueryString["pno"] != null ? Request.QueryString["pno"].Trim() : "test";
                sql = "INSERT INTO PR_Attachment VALUES('" + pNo + "','" + fupload.FileName + "','" + fupload.PostedFile.ContentLength / 1024 + "','" + sqlPath + "','" + txtDescription.Text.Trim() + "','" + userAccount + "','" + DateTime.Now.ToString("MM/dd/yy") + "')";
                DBBase.Update(sql);
                BindData();
                WebPage.ShowMessage("Upload file success!");
            }        protected void btnSave_Command(object sender, CommandEventArgs e)
            {
                if (ffFile.Text == "")
                {
                    WebPage.ShowMessage("Please select the file.");
                    return;
                }
                if (txtDescription.Text == "")
                {
                    WebPage.ShowMessage("Please add descriptions to the file you have uploaded.");
                    return;
                }            string Path = Server.MapPath(@"~/Uploads/");            //如果路径不存在,则创建
                if (System.IO.Directory.Exists(Path) == false)
                {
                    System.IO.Directory.CreateDirectory(Path);
                }            //组合路径,file.GetName()取得文件名
                Path = Path + fupload.FileName;
                sqlPath += fupload.FileName;
                //保存
                fupload.SaveAs(Path);            cookie = Request.Cookies["adUser"];
                userAccount = cookie == null ? "test" : cookie["account"].Trim();
                string pNo = Request.QueryString["pno"] != null ? Request.QueryString["pno"].Trim() : "test";
                sql = "INSERT INTO PR_Attachment VALUES('" + pNo + "','" + fupload.FileName + "','" + fupload.PostedFile.ContentLength / 1024 + "','" + sqlPath + "','" + txtDescription.Text.Trim() + "','" + userAccount + "','" + DateTime.Now.ToString("MM/dd/yy") + "')";
                DBBase.Update(sql);
                BindData();
                WebPage.ShowMessage("Upload file success!");
            }
        }
    }
      

  2.   

    不是按钮的问题是 function setValue(){
                document.getElementById("ffFile").value = document.getElementById("fupload").value;
            }的问题input file 由于安全的原因,不允许 js 或其他方式的赋值,只能是用户自己点击
      

  3.   

    <input type="button" value="Browsing" onclick="document.getElementById('fupload').click();"
                        class="ltBtn" />
    onclick="document.getElementById('fupload').click();"是这句的问题
      

  4.   

    function setValue(){
                document.getElementById("ffFile").value = document.getElementById("fupload").value;
            }解决方案 把这个逻辑在后台实现
      

  5.   

    http://blog.csdn.net/cpp2017/archive/2009/08/06/4418202.aspx给你贴一个慕白兄的例子吧 ~类似的有很多,可以自己搜索下。
      

  6.   

    我主要是想把浏览按钮的样式改掉。没想到会有这么多问题
    我现在后台可以获取路径,但保存到服务器只能用 SaveAs,用File.Copy 是不行的File流写入也不行。。 而 FileUpload.FileName 永远是 ""
      

  7.   

    原因已经和你说过了,更改浏览按钮的样式是不能了,只能用div做个层放在file 上让用户以为点的不是file ,从而实现文件上传http://www.cnblogs.com/cloudgamer/archive/2008/10/20/1314766.html还有很多jq插件 或 采用flash 上传的组件
      

  8.   

    找到完美方案:
    http://www.cnblogs.com/Charles2008/archive/2008/07/20/1247084.html结贴~