zoukankan      html  css  js  c++  java
  • 异步上传

    public const int CHUNK_SIZE = 4096;  
    public const string UPLOAD_URI = "http://localhost:55087/FileUpload.ashx?filename={0}&append={1}";  
    private Stream _data;  
    private string _fileName;  
    private long 
    _bytesTotal;  
    private long _bytesUploaded;    
    private void UploadFileChunk()  
    { 
        string uploadUri = ""; // Format the upload URI according to wether the it's the first chunk of the file 
        if (_bytesUploaded == 0) 
        { 
            uploadUri = String.Format(UPLOAD_URI,_fileName,0); // Dont't append 
        } 
        else if (_bytesUploaded < _bytesTotal) 
        { 
            uploadUri = String.Format(UPLOAD_URI, _fileName, 1); // append 
        } 
        else 
        { 
            return;  // Upload finished 
        } 
     
        byte[] fileContent = new byte[CHUNK_SIZE]; 
        _data.Read(fileContent, 0, CHUNK_SIZE); 
     
        WebClient wc = new WebClient(); 
        wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); 
        Uri u = new Uri(uploadUri); 
        wc.OpenWriteAsync(u, null, fileContent); 
        _bytesUploaded += fileContent.Length;  
    }    
     
    void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)  
    { 
        if (e.Error == null) 
        {    
            object[] objArr = e.UserState as object[]; 
            byte[] fileContent = objArr[0] as byte[]; 
            int bytesRead = Convert.ToInt32(objArr[1]); 
            Stream outputStream = e.Result; 
            outputStream.Write(fileContent, 0, bytesRead); 
            outputStream.Close(); 
            if (_bytesUploaded < _bytesTotal) 
            { 
                UploadFileChunk(); 
            } 
            else 
            { 
                // Upload complete 
            } 
        }  
    } 
    

      

    编辑器加载中...

  • 相关阅读:
    springboot的整合篇-springmvc,mybatis,数据库等
    快速搭建springboot工程
    Git常用命令
    SpringBoot整合通用Mapper
    SpringBoot项目部署
    实例化调用和静态方式调用的区别
    CentOS系统安装PHP7.3
    CentOS7部署LNMP环境
    Apache和Nginx的区别
    Linux系统下卸载MySQL
  • 原文地址:https://www.cnblogs.com/ysharp/p/2425564.html
Copyright © 2011-2022 走看看