kevinstonecode / blog Goto Github PK
View Code? Open in Web Editor NEWmy blog
my blog
雖然這篇的檔案室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)
一般在 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)
所以需要使用另外的方法
支援舊款瀏覽器的方法:(使用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的時候處理接收到的資料就完成了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.