檔案以二進流存入資料庫,且提供下載

來源:互聯網
上載者:User
上個星期做了一個內部郵件收發的,,且支援附件,,
為了做好這個附件確實找了很多資料,求助了很多人,,現把代碼貼出來,供大家討論,完善,以方便那些需要之士,

表結構table   name   myfile:   id,files(image),type(varchar(50)

上傳
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   System.Data.SqlClient;
using   System.IO;

public   partial   class   Image   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {

                Stream   fileDataStream   =   FileUpload1.PostedFile.InputStream;

                HttpPostedFile   file   =   FileUpload1.PostedFile;
                int   fileLength   =   FileUpload1.PostedFile.ContentLength;

                byte[]   fileData   =   new   byte[fileLength];
                fileDataStream.Read(fileData,   0,   fileLength);   //把檔案流填充到數組  

                string   fileType   =   Path.GetExtension(FileUpload1.PostedFile.FileName);//得到副檔名

                SqlConnection   cn   =   new   SqlConnection(ConfigurationManager.AppSettings[ "DbConnectionString "].ToString());
                cn.Open();

                SqlCommand   cmd   =   new   SqlCommand( "insert   myfile(files,type)   values(@files,@type) ",   cn);
                cmd.Parameters.AddWithValue( "@files ",   fileData);
                cmd.Parameters.AddWithValue( "@type ",   fileType);
                cmd.ExecuteNonQuery();
                cn.Close();
        }
}
<head   runat= "server ">
        <title> 上傳檔案到資料庫 </title>
</head>
<body>
        <form   id= "form1 "   runat= "server "   enctype= "multipart/form-data ">
        <div>
                <asp:FileUpload   ID= "FileUpload1 "   runat= "server "   />
                <asp:Button   ID= "Button1 "   runat= "server "   Text= "Button "   OnClick= "Button1_Click "   /> </div>
        </form>
</body>

下載

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   System.Data.SqlClient;
using   System.IO;

public   partial   class   Image2   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                  SqlConnection   cn   =   new   SqlConnection( "server=.;uid=sa;pwd=sa;database=test; ");     //資料庫連結
                cn.Open();

                SqlDataAdapter   da2   =   new   SqlDataAdapter( "select   *   from   myfile   where   id=   24 ",   cn);//讀出庫中的第5條資料
                DataSet   ds2   =   new   DataSet();
                da2.Fill(ds2);
                byte[]   b2   =   (byte[])ds2.Tables[0].Rows[0][ "files "];
                string   type   =   (string)ds2.Tables[0].Rows[0][ "type "];

                Response.Clear();
                string   Type   =   checktype(type);
                Response.AddHeader( "Content-Disposition ",   "attachment;   filename=abc "+type);

                Response.AddHeader( "Content-Length ",   b2.Length.ToString());
                Response.ContentType   =   Type;

                Response.BinaryWrite(b2);
                Response.End();

                string   FileName   =   ((LinkButton)sender).CommandArgument;
                Response.Clear();
                Response.ContentType   =   Type;
                Response.AddHeader( "Content-Disposition ",   "attachment;FileName= "   +   HttpUtility.UrlEncode(FileName,   System.Text.Encoding.UTF8));
                Response.WriteFile(FileName);
                Response.End();
        }
        private   string   checktype(string   filename)
        {
                string   ContentType;
                switch   (filename.Substring(filename.LastIndexOf( ". ")).Trim().ToLower())
                {
                        case   ".asf ":
                                ContentType   =   "video/x-ms-asf ";
                                break;
                        case   ".avi ":
                                ContentType   =   "video/avi ";
                                break;
                        case   ".doc ":
                                ContentType   =   "application/msword ";   break;
                        case   ".zip ":
                                ContentType   =   "application/zip ";   break;
                        case   ".xls ":
                                ContentType   =   "application/vnd.ms-excel ";   break;
                        case   ".gif ":
                                ContentType   =   "image/gif ";   break;
                        case   ".jpg ":
                                ContentType   =   "image/jpeg ";   break;
                        case   "jpeg ":
                                ContentType   =   "image/jpeg ";   break;
                        case   ".wav ":
                                ContentType   =   "audio/wav ";   break;
                        case   ".mp3 ":
                                ContentType   =   "audio/mpeg3 ";   break;
                        case   ".mpg ":
                                ContentType   =   "video/mpeg ";   break;
                        case   ".mepg ":
                                ContentType   =   "video/mpeg ";   break;
                        case   ".rtf ":
                                ContentType   =   "application/rtf ";   break;
                        case   ".html ":
                                ContentType   =   "text/html ";   break;
                        case   ".htm ":
                                ContentType   =   "text/html ";   break;
                        case   ".txt ":
                                ContentType   =   "text/plain ";   break;
                        default:
                                ContentType   =   "application/octet-stream ";
                                break;
                }
                return   ContentType;
        }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.