asp.net 上傳下載檔案

來源:互聯網
上載者:User

using System.IO;

  byte[] File = null;
        if (upf1.PostedFile.ContentLength > 0)
        {
            string[] FileSplite = upf1.PostedFile.FileName.Split('//');//取得檔案名稱
            fn = FileSplite[FileSplite.Length - 1];
            ft = upf1.PostedFile.ContentType;
            string faan = Server.MapPath(".") + "//upload//" + fn;
            upf1.PostedFile.SaveAs(faan);
            FileInfo fi = new FileInfo(faan);
            FileStream fs = fi.OpenRead();
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
            File = bytes;
            fs.Flush();
            fs.Close();
            fi = null;
            fi = new FileInfo(faan);
            fi.Delete();
            fi = null;
        }
        string conn = ConfigurationManager.AppSettings["Oracle9iConnStr"];
        OracleConnection MyConnection = new OracleConnection(conn);
        OracleCommand MyCommand = new OracleCommand();
        MyCommand.Connection = MyConnection;
        OracleDataAdapter objDataAdapter = new OracleDataAdapter();
        objDataAdapter.SelectCommand = MyCommand;
        DataSet Sett = new DataSet();
        string str12 = "select * from sz_disk where fpath='" + Request.Form["fpath"].Trim().ToString() + "' and fname='" + Request.Form["fname"].Trim().ToString().Replace("'", "") + "' and id<>" + id;
        MyCommand.CommandText = str12;
        objDataAdapter.Fill(Sett, "c");
        if (Sett.Tables["c"].Rows.Count > 0)
        {
            Response.Write("<scr" + "ipt language=javascript>alert('已有此檔案!');</scr" + "ipt>");
            return;
        }
        OracleCommandBuilder cb = new OracleCommandBuilder(objDataAdapter);

        string str2 = "select * from sz_disk where id=" + id;
        MyCommand.CommandText = str2;
        objDataAdapter.Fill(Sett, "b");
        DataRow dr1 = Sett.Tables["b"].Rows[0];
        //dr1["fpath"]=Request.Form["fpath"].Trim().ToString();
        if (Request.Form["fname"].Trim().ToString().Replace("'", "") != "")
        {
            dr1["fname"] = Request.Form["fname"].Trim().ToString().Replace("'", "");
        }
        else
        {
            dr1["fname"] = System.DBNull.Value;
        }
        if (Request.Form["keyword"].Trim().ToString() != "")
        {
            dr1["keyword"] = Request.Form["keyword"].Trim().ToString();
        }
        else
        {
            dr1["keyword"] = System.DBNull.Value;
        }
        //dr1["oper"]=Request.Form["oper"].Trim().ToString();
        dr1["ctime"] = DateTime.Now;
        if (File != null)
        {
            dr1["filedata"] = File;
            dr1["filetype1"] = ft;
            dr1["Filename"] = fn;
        }
        dr1["fileimage"] = Request.Form["fi"].ToString();
        switch (Request.Form["fi"].ToString())
        {
            case "doc.jpg":
                dr1["filetype"] = "word文檔";
                break;
            case "ppt.jpg":
                dr1["filetype"] = "示範文檔";
                break;
            case "htm.jpg":
                dr1["filetype"] = "網頁檔案";
                break;
            case "swf.jpg":
                dr1["filetype"] = "flash檔案";
                break;
            case "jpg.jpg":
                dr1["filetype"] = "圖片檔案";
                break;
            case "txt.jpg":
                dr1["filetype"] = "文字檔";
                break;
            case "mdb.jpg":
                dr1["filetype"] = "access資料庫";
                break;
            case "xls.jpg":
                dr1["filetype"] = "excel文檔";
                break;
            case "mp3.jpg":
                dr1["filetype"] = "音頻檔案";
                break;
            case "zip.jpg":
                dr1["filetype"] = "壓縮檔";
                break;
            case "png.jpg":
                dr1["filetype"] = "fireworks檔案";
                break;
            default:
                dr1["filetype"] = "未知類型";
                break;
        }
        if (Request.Form["content"].Trim().ToString().Replace("'", "") != "")
        {
            dr1["content"] = Request.Form["content"].Trim().ToString().Replace("'", "");
        }
        else
        {
            dr1["content"] = System.DBNull.Value;
        }
        //Response.Write("<scr"+"ipt>alert('"+Session["name"].ToString()+"');</scr"+"ipt>");
        //Sett.Tables["b"].Rows.Add(dr1);
        objDataAdapter.Update(Sett, "b");
        Sett.Clear();
        Response.Write("<scr" + "ipt language=javascript>alert('儲存成功!');window.opener.location.reload();window.opener='';window.close();</scr" + "ipt>");
   

 

 

 public void file_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        string conn = ConfigurationManager.AppSettings["Oracle9iConnStr"];
        string StrSql1 = "select * from sz_disk where id=" + id;
        //Response.Write(StrSql1);
        //Response.End();
        OracleConnection MyConnection = new OracleConnection(conn);
        OracleCommand MyCommand = new OracleCommand();
        MyCommand.Connection = MyConnection;
        OracleDataAdapter objDataAdapter = new OracleDataAdapter();
        objDataAdapter.SelectCommand = MyCommand;
        DataSet objectSet1 = new DataSet();
        MyCommand.CommandText = StrSql1;
        objDataAdapter.Fill(objectSet1, "b");
        DataRow dr1 = objectSet1.Tables["b"].Rows[0];
        Response.Clear();
        //byte[] File=null;
        //File=(byte[])dr1["filedata"];
        Response.ContentType = dr1["filetype1"].ToString();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(dr1["filename"].ToString(), System.Text.Encoding.UTF8));
        Response.BinaryWrite((byte[])dr1["filedata"]);
        Response.End();
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.