[問題] 一次下載多筆資料庫的image資料

看板C_Sharp (C#)作者 (4545454554)時間9年前 (2016/05/20 15:18), 9年前編輯推噓0(0012)
留言12則, 3人參與, 最新討論串1/2 (看更多)
各位好 我用的資料庫是Microsoft SQL Server 我用類似下面的語法可以把某筆image資料 下載成一個檔案 http://goo.gl/2bWGL4 但這樣每次只能下載一個檔案 請問 有辦法一次下載多筆嗎? 或者是一次下載多筆檔案所壓縮而成的zip檔 謝謝 --------------------------------------------------- byte[] xfile = null; string docupath = Request.PhysicalApplicationPath; xfile = wc.DownloadData(docupath + "uploads\\qrcode.png"); string xfileName = System.IO.Path.GetFileName (docupath + "uploads\\qrcode.png"); HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(xfileName)); HttpContext.Current.Response.ContentType = "application/octet-stream"; //二進位方式 HttpContext.Current.Response.BinaryWrite(xfile); //內容轉出作檔案下載 HttpContext.Current.Response.End(); ---------------------------------------------------------------- 我後來使用DotNetZip來壓縮檔案 https://dotnetzip.codeplex.com/ 右邊是官方提供的一些 C# Examples https://goo.gl/FNjqug 作法大致如下 先宣告一個MemoryStream MemoryStream ms = new MemoryStream(); 再宣告一個ZipFile 透過AddEntry 把從資料庫撈出的檔案加進去ZipFile 接著用Save把壓縮檔存到一開始宣告的MemoryStream中 ZipFile zip = new ZipFile()); zip.AddEntry("檔名",stream或byte[]); // stream或byte[]就是從資料庫撈出的檔案 zip.Save(ms); 最後透過Response.BinaryWrite 把一開始宣告的MemoryStream下載下來 Response.BinaryWrite(ms.ToArray()); 以上供有相同問題的人參考 謝謝大家幫忙 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.127.81.14 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1463728684.A.825.html

05/21 03:22, , 1F
你可以把圖檔轉base64去存在DB內 存NoSQL也是不錯的選擇
05/21 03:22, 1F

05/21 03:23, , 2F
然後寫個WS去串連DB資料 最後你看你要取單張 還是多張壓縮
05/21 03:23, 2F

05/21 03:24, , 3F
當然你也可以不用這麼麻煩 直接先把圖檔都取出來放在記憶體
05/21 03:24, 3F

05/21 03:25, , 4F
然後再轉換後做壓縮 然後下載
05/21 03:25, 4F

05/21 17:23, , 5F
謝謝 如果是後面那種做法 有程式碼範例可供我參考嗎?
05/21 17:23, 5F

05/21 17:25, , 6F
或者是關鍵字 我再自己去google
05/21 17:25, 6F

05/21 19:08, , 7F
你要先看懂你這段程式的意思,倒數第二行是把HttpResponse(
05/21 19:08, 7F

05/21 19:08, , 8F
下載的檔案)存到xfile(實體檔案)
05/21 19:08, 8F

05/21 19:11, , 9F
↑應該說用xfile寫入
05/21 19:11, 9F

05/21 19:11, , 10F
所以你要下載一個zip就是你要先另外把多個實體檔案讀進
05/21 19:11, 10F

05/21 19:12, , 11F
memory,再做成一個壓縮檔,再拿來用就好了
05/21 19:12, 11F

05/21 19:16, , 12F
至於怎麼把檔案從DB讀出來,那就要看你怎麼存、怎麼連了
05/21 19:16, 12F
※ 編輯: kisha024 (140.127.81.14), 06/09/2016 18:08:00
文章代碼(AID): #1NFheiWb (C_Sharp)
文章代碼(AID): #1NFheiWb (C_Sharp)