html5檔案上傳
htmL程式碼:
- <div class=“row”>
- <input type=“file” name=“fileToUpload” id=“fileToUpload” onchange=“fileSelected();”/>
- </div>
- <div id=“fileSize”></div>
- <div id=“fileType”></div>
- <div class=“row”>
- <input type=“button” onclick=“uploadFile()” value=“上傳” />
- </div>
- <div id=“progressNumber”></div>
- <progress max=“100” id=“progre” value=“0”></progress>
- <br />
- <asp:Image ID=“Image1” Style=“max-width:130px” runat=“server” />
- <asp:HiddenField ID=“hfimg” value=“” runat=“server” />
JS程式碼:
- <script type=“text/javascript”>
- function fileSelected() {
- var file = document.getElementById(`fileToUpload`).files[0];
- if (file) {
- var fileSize = 0;
- if (file.size > 1024 * 1024)
- fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + `MB`;
- else
- fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + `KB`;
- document.getElementById(`fileSize`).innerHTML = `檔案大小: ` + fileSize;
- document.getElementById(`fileType`).innerHTML = `檔案型別: ` + file.type;
- }
- }
- function uploadFile() {
- var fd = new FormData();
- fd.append(“fileToUpload”, document.getElementById(`fileToUpload`).files[0]);
- var xhr = new XMLHttpRequest();
- xhr.upload.addEventListener(“progress”, uploadProgress, false);
- xhr.addEventListener(“load”, uploadComplete, false);
- xhr.addEventListener(“error”, uploadFailed, false);
- xhr.addEventListener(“abort”, uploadCanceled, false);
- xhr.open(“POST”, “/handler/h5upload.ashx”);
- xhr.send(fd);
- }
- function uploadProgress(evt) {
- if (evt.lengthComputable) {
- var percentComplete = Math.round(evt.loaded * 100 / evt.total);
- document.getElementById(`progressNumber`).innerHTML = percentComplete.toString() + `%`;
- document.getElementById(`progre`).value = percentComplete;
- }
- else {
- document.getElementById(`progressNumber`).innerHTML = `unable to compute`;
- }
- }
- function uploadComplete(evt) {
- /* This event is raised when the server send back a response */
- var json =eval(“(“+ evt.target.responseText+“)”);
- if (json.status==“no”) {
- alert(json.info);
- }else{
- document.getElementById(“<%=Image1.ClientID%>”).src = json.path;
- document.getElementById(“<%=hfimg.ClientID%>”).value = json.path;
- }
- }
- function uploadFailed(evt) {
- alert(“There was an error attempting to upload the file.”);
- }
- function uploadCanceled(evt) {
- alert(“The upload has been canceled by the user or the browser dropped the connection.”);
- }
- </script>
後臺ashx程式碼:
- context.Response.ContentType = “text/plain”;
- HttpPostedFile file =context. Request.Files[“fileToUpload”];
- String fileName = file.FileName;
- String fileExt = Path.GetExtension(fileName).ToLower();
- //定義允許上傳的副檔名
- Hashtable extTable = new Hashtable();
- extTable.Add(“image”, “gif,jpg,jpeg,png,bmp”);
- extTable.Add(“flash”, “swf,flv”);
- extTable.Add(“media”, “swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb”);
- extTable.Add(“file”, “doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2”);
- if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[“image”]).Split(`,`), fileExt.Substring(1).ToLower()) == -1)
- {
- context.Response.Write(LitJson.JsonMapper.ToJson(new { status=“no”, info = “上傳副檔名是不允許的副檔名。
- 只允許” + ((String)extTable[“image“]) + “格式。” }));
- return;
- }
- string dirpath = “/upload/”+DateTime.Now.ToString(“yyyyMMdd”)+“/”;
- if (!Directory.Exists(context.Server.MapPath( dirpath)))
- {
- Directory.CreateDirectory(context.Server.MapPath(dirpath));
- }
- String newFileName = DateTime.Now.ToString(“yyyyMMddHHmmss_ffff”, DateTimeFormatInfo.InvariantInfo) + fileExt;
- file.SaveAs(context.Server.MapPath(dirpath) + newFileName);
- context.Response.Write(LitJson.JsonMapper.ToJson(new { status=“ok”,path = dirpath+newFileName}));
瀏覽(505) | 評論(0) | 分類: javascript
相關文章
- Html5 檔案上傳HTML
- HTML5拖拽檔案上傳HTML
- HTML5檔案上傳元件的深度剖析HTML元件
- php檔案上傳之多檔案上傳PHP
- HTML5 進階系列:檔案上傳下載HTML
- 【方法】Html5實現檔案非同步上傳HTML非同步
- HTML5向量實現檔案上傳進度條HTML
- 向量HTML5實現檔案上傳進度條HTML
- 單個檔案上傳和批量檔案上傳
- 檔案上傳
- SpringMVC 單檔案上傳與多檔案上傳SpringMVC
- Java大檔案上傳、分片上傳、多檔案上傳、斷點續傳、上傳檔案minio、分片上傳minio等解決方案Java斷點
- 檔案上傳之三基於flash的檔案上傳
- 前端大檔案上傳/分片上傳前端
- Flask——檔案上傳Flask
- PHP上傳檔案PHP
- JavaScript 檔案上傳JavaScript
- Git上傳檔案Git
- YII檔案上傳
- 檔案上傳概述
- beego上傳檔案Go
- 上傳檔案流程
- 上傳EXCLE檔案
- PHP 檔案上傳PHP
- 檔案上傳漏洞
- HTML5檔案上傳外掛HuploadifyV2.1釋出HTML
- Resumable.js – 基於 HTML5 File API 的檔案上傳JSHTMLAPI
- MVC檔案上傳 - 使用Request.Files上傳多個檔案MVC
- .NET Core 如何上傳檔案及處理大檔案上傳
- Django檔案上傳 -- 適用於單一小檔案上傳Django
- HTML5檔案上傳外掛 Huploadify V2.1釋出HTML
- 上傳檔案的陷阱
- HTTP檔案上傳原理HTTP
- 上傳檔案專題
- PHP 分片上傳檔案PHP
- Java Web 檔案上傳JavaWeb
- WEB漏洞——檔案上傳Web
- Aliyun Oss 上傳檔案