1. 程式人生 > >WEB版一次選擇多個檔案進行批量上傳(Plupload)的解決方案

WEB版一次選擇多個檔案進行批量上傳(Plupload)的解決方案

複製程式碼
    /// <summary>
    /// uploadFiles 的摘要說明
    /// </summary>
    public class uploadFiles : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            UploadFile(context);
        }

        
public bool IsReusable { get { return false; } } public void UploadFile(HttpContext context) { context.Response.CacheControl = "no-cache"; string s_rpath = FileHelper.GetUploadPath();//@"E:\My Documents\Visual Studio 2008\WebSites\SWFUpload\demos\applicationdemo.net";
string Datedir = DateTime.Now.ToString("yy-MM-dd"); string updir = s_rpath + "\\" + Datedir; string extname = string.Empty; string fullname = string.Empty; string filename = string.Empty; if (context.Request.Files.Count > 0
) { try { for (int j = 0; j < context.Request.Files.Count; j++) { HttpPostedFile uploadFile = context.Request.Files[j]; int offset =Convert.ToInt32( context.Request["chunk"]); //當前分塊 int total =Convert.ToInt32(context.Request["chunks"]);//總的分塊數量 string name = context.Request["name"]; //檔案沒有分塊 if (total == 1) { if (uploadFile.ContentLength > 0) { if (!Directory.Exists(updir)) { Directory.CreateDirectory(updir); } extname = Path.GetExtension(uploadFile.FileName); fullname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(); filename = uploadFile.FileName; uploadFile.SaveAs(string.Format("{0}\\{1}", updir, filename)); } } else { //檔案 分成多塊上傳 fullname = WriteTempFile(uploadFile, offset); if (total-offset==1) { //如果是最後一個分塊檔案 ,則把檔案從臨時資料夾中移到上傳檔案 夾中 System.IO.FileInfo fi = new System.IO.FileInfo(fullname); string oldFullName = string.Format("{0}\\{1}", updir, uploadFile.FileName); FileInfo oldFi = new FileInfo(oldFullName); if (oldFi.Exists) { //檔名存在則刪除舊檔案 oldFi.Delete(); } fi.MoveTo(oldFullName); } } } } catch (Exception ex) { context.Response.Write("Message" + ex.ToString()); } } } /// <summary> /// 儲存臨時檔案 /// </summary> /// <param name="uploadFile"></param> /// <param name="chunk"></param> /// <returns></returns> private string WriteTempFile(HttpPostedFile uploadFile, int chunk) { string tempDir = FileHelper.GetTempPath(); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } string fullName = string.Format("{0}\\{1}.part", tempDir, uploadFile.FileName); if (chunk==0) { //如果是第一個分塊,則直接儲存 uploadFile.SaveAs(fullName); } else { //如果是其他分塊檔案 ,則原來的分塊檔案,讀取流,然後檔案最後寫入相應的位元組 FileStream fs = new FileStream(fullName, FileMode.Append); if (uploadFile.ContentLength>0) { int FileLen = uploadFile.ContentLength; byte[] input = new byte[FileLen]; // Initialize the stream. System.IO.Stream MyStream = uploadFile.InputStream; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); fs.Write(input,0,FileLen); fs.Close(); } } return fullName; } }
複製程式碼

相關推薦

WEB選擇檔案進行批量(Plupload)的解決方案

/// <summary> /// uploadFiles 的摘要說明 /// </summary> public class uploadFiles : IHttpHandler { public void Process

WEB選擇檔案進行批量(swfupload)的解決方案

功能完全支援ie和firefox瀏覽器!       一般的WEB方式檔案上傳只能使用FileUpload控制元件進行一個檔案一個檔案的進行上傳,就算是批量上傳,也要把檔案一個一個的新增到頁面,無法如windows程式一樣,一次選擇多個檔案進行批量上傳。這樣在某些應用上就

WEB選擇檔案進行批量(WebUploader)的解決方案

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebUploaderFileByBaidu2.aspx.cs" Inherits="WebApplication1.WebUploaderFileByBaidu2" %> <

下載檔案解決思路-JS

一次下載多個檔案的解決思路(iframe) - Eric 真實經歷 最近開發專案需要做檔案下載,想想挺簡單的,之前也做過,後臺提供下載介面,前端使用window.location.href就行了唄。不過開發的時候發現,有些檔案有附屬檔案,點選 下載按鈕 需要下載兩個檔案,而且不能使用壓縮包的形式。想想

如何用shell讀取檔案內容

在寫shell指令碼的時候,因為shell本身的資料結構很簡單,連二維陣列都不能支援,所以配置檔案的形式也不能過於複雜(個人看法,有什麼意見請說),因此需要寫多個配置檔案來滿足業務。 通常情況下,我們用shell讀取一個配置檔案是這樣寫的: while re

linux下cp檔案

linux下使用cp命令拷貝多個檔案有幾種方法,一是使用萬用字元,二是使用管道 一.萬用字元的使用 萬用字元是一種特殊語句,主要有星號(*)和問號(?),用來模糊搜尋檔案。主要的萬用字元有: *         匹配任意長度的字串 ?        匹配一個長度的字元

JAVA 如何下載檔案

https://zhidao.baidu.com/question/1446055057527183940.html import java.io.BufferedInputStream; import java.io.File; import java.io.FileN

替換詞,批量替換字串(使用不了的解決辦法)

三天前上CSDN,發現有人回覆批量替換多個詞的工具使用不了。下載下來試用了一下,確實報錯,這兩天打算做個網頁版本的,排版佈局還沒弄好。 上貼回覆以前工具使用不了的解決辦法:下載32位的jre,然後配置JAVA_HOME。 2、下載完畢解壓後,假如路徑(自

jquery 選擇2元素 元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x

下載文件

itl cnblogs script log logs html 文件 download nload 一次下載多個文件 JavaScript多文件下載一次下載多個文件

(轉)Spring文件,包括選中文件

bmi while .html span cto input 獲取文件 dex asn 背景: http://www.cnblogs.com/lixuwu/p/8495275.html已經實現了單文件的上傳和下載,多文件的上傳是另一種情景,這裏記錄下來 實現過程

maven 打包maven專案

maven 一次打包多個maven專案。 使用場景 一個專案由多個子專案組成,每個子專案也是一個maven專案。每次打包需要打包每個子專案,很麻煩,其實可以通過配置一個頂級的pom.xml檔案來解決這個問題,只需要打包頂層的maven專案,即可。如果一個專案有多個子專案的pom.

List集合刪除元素

JAVA中迴圈遍歷list有三種方式:   for迴圈、增強for迴圈(也就是常說的foreach迴圈)、iterator遍歷。 1、for迴圈遍歷list for(int i=0;i<list.size();i++){ if(list.get(i).equals

QFileDialog如何選擇檔案

   QFileDialog 按說是預設選擇多個檔案,但是我的ctrl 和shift鍵都不管用,每次只能選擇一個檔案,很惆悵....    網上大部分都是問如何選擇多個檔案和資料夾的.後來發現:   add_file_d

Android6.0及以上版本請求許可權的處理方式

final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124; private void insertDummyContactWrapper() { List<Stri

Python列表刪除重複元素

(1)刪除列表中所有'a': l = [0,1,'a',2,'a','a',3,4] l = [x for x in l if x != 'a'] print(l) >>[0,1,2,3,4] (2)刪除列表中所有'a'和‘b’: delete_list = ['a','b'

Android 仿PC端QQ自由截圖,可支援區域

仿PC端QQ截圖,可任意截圖,這裡只做了矩形這一種形狀,可同時支援擷取多個區域,支援撤銷上次截圖,重新擷取。 實現原理: 自定義SurfaceView,在SurfaceView上繪製具有一個可拉伸,移動的矩形框,當點選截圖按鈕後,計算矩形

Ubuntu系統編寫shell指令碼程式安裝軟體包

#!/bin/sh sudo apt-get install gcc g++ python -y sudo apt-get install gcc g++ python python-dev -y sudo apt-get install mercurial -y sudo apt-get install b

使用JDBC插入表、條記錄

程式碼如下: public static void insertBatch() { int count[]; int count1[]; Boolean isinsert = false; Connection con = null; PreparedS

Android 6.0動態申請許可權(申請

Android一次申請多個動態許可權 昨天做一個錄音功能的程式,發現在開啟Mic的時候發生了程式崩潰,但是錯誤提示裡面居然沒有提示許可權禁止。  解決的方法有:1.手動去選擇“應用”–>“程式”–>找到那個程式新增許可權,後面程式就可以正常使用了 2.設定動態請