Giter VIP home page Giter VIP logo

blog's People

Watchers

 avatar  avatar

blog's Issues

On Asp.net Create and put files into zip in memory using DotNetZip

這篇主要是用NPOI產生多個Excel,中間不產生實體檔案而是使用stream的方式,並且放進zip檔案後下載

使用版本

  1. .NET Framework 4.6.1
  2. DotNetZip 1.13.3
  3. NPOI 2.4.1

雖然這篇的檔案室Excel,不過我想使用其他檔案應該也是一樣的意思

第一步,先簡單產生兩個檔案

        Dim workbook As New XSSFWorkbook()
        Dim sheet As ISheet = workbook.CreateSheet()
        Dim headerRow As IRow = sheet.CreateRow(0)
        headerRow.CreateCell(0).SetCellValue("test")
        headerRow.CreateCell(1).SetCellValue("測試中文")

        Dim workbook2 As New XSSFWorkbook()
        Dim sheet2 As ISheet = workbook2.CreateSheet()
        Dim headerRow2 As IRow = sheet2.CreateRow(0)
        headerRow2.CreateCell(0).SetCellValue("test2")
        headerRow2.CreateCell(1).SetCellValue("測試中文2")

個別寫到stream裡面

        Dim ms As MemoryStream = New MemoryStream()
        '' leaves MemoryStream open
        workbook.Write(ms, True)
        ms.Position = 0

        Dim ms2 As MemoryStream = New MemoryStream()
        workbook2.Write(ms2, True)
        ms2.Position = 0

把MemoryStreamk的position設定成0很重要,否則檔案會變成0 kB (目前還沒搞懂原因)

接著 New 一個 ZipFile

zip As ZipFile = New ZipFile()

設定編碼格式,處理中文問題

            zip.AlternateEncoding = Encoding.GetEncoding("utf-8")
            zip.AlternateEncodingUsage = ZipOption.Always

加入stream

            zip.AddEntry("中文檔名.xlsx", ms)
            zip.AddEntry("testfilename.xlsx", ms2)

存檔,收工

zip.Save(Response.OutputStream)

Source Code

DemoMutiExcelToZip

參考資料

  1. Create Zip file in memory using DotNetZip
  2. ASP.NET 案例分享:從 NPOI 匯出的 Excel 資料流建立附件並寄送
  3. MemoryStream seems be closed after NPOI workbook.write?

On Asp.net Webform Using jQuery AJAX to upload file (support IE < 10)

在Asp.net Webform上使用jQuery AJAX上傳檔案

一般在 Webform 上傳檔案都是用它本身的控制項 FileUpload
但是如果使用者選擇了檔案後沒有上傳,而是去做其他動作造成PostBack的時候,檔案一樣還是會Post到Server上,這時候如果檔案很大就會是一場災難,為了避免這種情形,所以將上傳檔案的部分改成用jQuery AJAX上傳。
首先,在HTML的部分先新增兩個input:

<input id="fileUpload" type="file" name="fileUpload" accept=".pdf,.doc,.docx"/>
<input id="btnUploadFile" type="button" value="上傳" />

按下button時,jQuery的部分,先新增一個FormData

var data = new FormData();

再將檔案塞進去

var files = $('#fileUpload').get(0).files;

if (files.length > 0) {
    data.append('UploadedFile', files[0]);
}

最後再用AJAX post出去給server接收,至於後端的部分可以參考資料1或[source code}(https://github.com/KevinStoneCode/DemoFileUploadByAJAX)

但是這個方法有個缺點,FormData不支援IE7-9

所以需要使用另外的方法

支援舊款瀏覽器的方法:(使用iframe)
在HTML部分改用form

<form action='URL' method='post' enctype='multipart/form-data' target='Uploadedfrm'>
    <input id="fileUpload" type="file" name="fileUpload" accept=".pdf,.doc,.docx"/>
    <input id="btnUploadFile" type="submit" value="上傳" />
</form>

再新增一個iframe來接收server回傳的資料

<iframe id="Uploadedfrm" name="Uploadedfrm" style="display: none"></iframe>

基本上後端的程式不需要修改,最後在iframe onload的時候處理接收到的資料就完成了

source code

DemoFileUploadByAJAX

參考資料

  1. 【.Net MVC】File Upload using jQuery AJAX
  2. How can I upload files asynchronously?
  3. How to make Asynchronous(AJAX) File Upload using iframe?
  4. How to create byte array from HttpPostedFile

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.