/// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="filePath">文件的完整路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadFile(string filePath, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromPath(key, null, filePath);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
예제 #2
0
        public ResumeUploader(HttpManager httpManager, ResumeRecorder recorder, string recordKey, Stream stream,
                              string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            this.httpManager         = httpManager;
            this.resumeRecorder      = recorder;
            this.recordKey           = recordKey;
            this.fileStream          = stream;
            this.key                 = key;
            this.storage             = IsolatedStorageFile.GetUserStoreForApplication();
            this.uploadOptions       = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions;
            this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                uploadOptions.ProgressHandler(key, 1.0);

                if (this.fileStream != null)
                {
                    try
                    {
                        this.fileStream.Close();
                    }
                    catch (Exception) { }
                }
                if (upCompletionHandler != null)
                {
                    upCompletionHandler(key, respInfo, response);
                }
            });
            this.httpManager.setAuthHeader("UpToken " + token);
            this.chunkBuffer = new byte[Config.CHUNK_SIZE];
        }
        /// <summary>
        /// 以表单方式上传数据流
        /// </summary>
        /// <param name="stream">文件数据流</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadStream(Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromStream(key, null, stream);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
        public void uploadData(byte[] data, int offset, int count, string key, string token,
                               UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromSlice(key, null, data, offset, count);

            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
예제 #5
0
        public ResumeUploader(HttpManager httpManager, ResumeRecorder recorder, string recordKey, Stream stream,
            string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            this.httpManager = httpManager;
            this.resumeRecorder = recorder;
            this.recordKey = recordKey;
            this.fileStream = stream;
            this.key = key;
            this.storage = IsolatedStorageFile.GetUserStoreForApplication();
            this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions;
            this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                uploadOptions.ProgressHandler(key, 1.0);

                if (this.fileStream != null)
                {
                    try
                    {
                        this.fileStream.Close();
                    }
                    catch (Exception) { }
                }
                if (upCompletionHandler != null)
                {
                    upCompletionHandler(key, respInfo, response);
                }
            });
            this.httpManager.setAuthHeader("UpToken " + token);
            this.chunkBuffer = new byte[Config.CHUNK_SIZE];
        }
예제 #6
0
        /// <summary>
        /// 上传文件流
        /// </summary>
        /// <param name="stream">文件流对象</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件流
        public void uploadStream(Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            long fileSize = stream.Length;

            if (fileSize <= Config.PUT_THRESHOLD)
            {
                new FormUploader().uploadStream(stream, key, token, uploadOptions, upCompletionHandler);
            }
            else
            {
                if (this.resumeRecorder == null)
                {
                    string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                    this.resumeRecorder = new ResumeRecorder(home);
                }

                string recorderKey = null;

                if (this.keyGenerator == null)
                {
                    recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(key));
                }
                else
                {
                    recorderKey = this.keyGenerator();
                }

                new ResumeUploader(this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
            }
        }
예제 #7
0
 /// <summary>
 /// 以表单方式上传字节数据
 /// </summary>
 /// <param name="data">字节数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadData(byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromBytes(key, null, data);
     // 此处未设置FormFile.ContentType,稍后设置(在upload中已设置) @fengyh 2016-08-17 15:03
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
예제 #8
0
        /// <summary>
        /// 上传沙盒文件
        /// </summary>
        /// <param name="filePath">沙盒文件全路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件
        public void uploadFile(string filePath, string key, string token,
                               UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            try
            {
                long     fileSize = 0;
                FileInfo s        = new FileInfo(filePath);
                fileSize = s.Length;

                //判断文件大小,选择上传方式
                if (fileSize <= Config.PUT_THRESHOLD)
                {
                    new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler);
                }
                else
                {
                    string recorderKey = null;
                    if (this.keyGenerator != null)
                    {
                        recorderKey = this.keyGenerator();
                    }
                    new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
                }
            }
            catch (Exception ex)
            {
                if (upCompletionHandler != null)
                {
                    upCompletionHandler(key, ResponseInfo.fileError(ex), null);
                }
            }
        }
예제 #9
0
 /// <summary>
 /// 上传沙盒文件
 /// </summary>
 /// <param name="filePath">沙盒文件全路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传结果处理器</param>
 #region   沙盒文件
 public void uploadFile(string filePath, string key, string token,
                        UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     try
     {
         long fileSize = 0;
         using (IsolatedStorageFileStream s = new IsolatedStorageFileStream(filePath, FileMode.Open,
                                                                            IsolatedStorageFile.GetUserStoreForApplication()))
         {
             fileSize = s.Length;
         }
         //判断文件大小,选择上传方式
         if (fileSize <= Config.PUT_THRESHOLD)
         {
             new FormUploader().uploadFile(this.httpManager, filePath, key, token, uploadOptions, upCompletionHandler);
         }
         else
         {
             string recorderKey = null;
             if (this.keyGenerator != null)
             {
                 recorderKey = this.keyGenerator();
             }
             new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
         }
     }
     catch (Exception ex)
     {
         if (upCompletionHandler != null)
         {
             upCompletionHandler(key, ResponseInfo.fileError(ex), null);
         }
     }
 }
        /// <summary>
        /// 以表单方式上传字节数据
        /// </summary>
        /// <param name="data">字节数据</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadData(byte[] data, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            HttpFormFile fFile = HttpFormFile.NewFileFromBytes(key, null, data);

            // 此处未设置FormFile.ContentType,稍后设置(在upload中已设置) @fengyh 2016-08-17 15:03
            upload(fFile, key, token, uploadOptions, upCompletionHandler);
        }
예제 #11
0
 /// <summary>
 /// 上传沙盒文件
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="filePath">沙盒文件的完整路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadFile(HttpManager httpManager, string filePath, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.File = filePath;
     postArgs.FileName = Path.GetFileName(filePath);
     httpManager.FileContentType = PostContentType.FILE;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
예제 #12
0
        /// <summary>
        /// 上传沙盒文件
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="filePath">沙盒文件的完整路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadFile(HttpManager httpManager, string filePath, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.File               = filePath;
            postArgs.FileName           = Path.GetFileName(filePath);
            httpManager.FileContentType = PostContentType.FILE;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
예제 #13
0
 /// <summary>
 /// 以表单方式上传字节数据
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="data">字节数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadData(HttpManager httpManager, byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.Data = data;
     if (key != null)
     {
         postArgs.FileName = key;
     }
     httpManager.FileContentType = PostContentType.BYTES;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
예제 #14
0
 /// <summary>
 /// 以表单方式上传数据流
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="stream">文件数据流</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadStream(HttpManager httpManager, Stream stream, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     PostArgs postArgs = new PostArgs();
     postArgs.Stream = stream;
     if (key != null)
     {
         postArgs.FileName = key;
     }
     httpManager.FileContentType = PostContentType.STREAM;
     upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
 }
예제 #15
0
        /// <summary>
        /// 以表单方式上传数据流
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="stream">文件数据流</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadStream(HttpManager httpManager, Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.Stream = stream;
            if (key != null)
            {
                postArgs.FileName = key;
            }
            httpManager.FileContentType = PostContentType.STREAM;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
예제 #16
0
        /// <summary>
        /// 以表单方式上传字节数据
        /// </summary>
        /// <param name="httpManager">HttpManager对象</param>
        /// <param name="data">字节数据</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public void uploadData(HttpManager httpManager, byte[] data, string key,
                               string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            PostArgs postArgs = new PostArgs();

            postArgs.Data = data;
            if (key != null)
            {
                postArgs.FileName = key;
            }
            httpManager.FileContentType = PostContentType.BYTES;
            upload(httpManager, postArgs, key, token, uploadOptions, upCompletionHandler);
        }
예제 #17
0
        public void uploadFile(string filePath, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            try
            {
                if(upCompletionHandler==null)
                {
                    upCompletionHandler = DefaultUpCompletionHandler;
                }

                long fileSize = 0;
                FileInfo s = new FileInfo(filePath);
                fileSize = s.Length;

                //判断文件大小,选择上传方式
                if (fileSize <= Config.PUT_THRESHOLD)
                {
                    new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler);
                }
                else
                {
                    if(this.resumeRecorder==null)
                    {
                        string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                        this.resumeRecorder = new ResumeRecorder(home);
                    }

                    string recorderKey = null;

                    if (this.keyGenerator == null)
                    {
                        recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(filePath + key));
                    }
                    else
                    {
                        recorderKey = this.keyGenerator();
                    }

                    new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
                }
            }
            catch (Exception ex)
            {
                if (upCompletionHandler != null)
                {
                    upCompletionHandler(key, ResponseInfo.fileError(ex), null);
                }
            }
        }
예제 #18
0
        /// <summary>
        /// 上传沙盒文件
        /// </summary>
        /// <param name="filePath">沙盒文件全路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件
        public void uploadFile(string filePath, string key, string token,
                               UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            try
            {
                if (upCompletionHandler == null)
                {
                    upCompletionHandler = DefaultUpCompletionHandler;
                }

                long     fileSize = 0;
                FileInfo s        = new FileInfo(filePath);
                fileSize = s.Length;

                //判断文件大小,选择上传方式
                if (fileSize <= Config.PUT_THRESHOLD)
                {
                    new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler);
                }
                else
                {
                    if (this.resumeRecorder == null)
                    {
                        string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                        this.resumeRecorder = new ResumeRecorder(home);
                    }

                    string recorderKey = null;

                    if (this.keyGenerator == null)
                    {
                        recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(filePath + key));
                    }
                    else
                    {
                        recorderKey = this.keyGenerator();
                    }

                    new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
                }
            }
            catch (Exception ex)
            {
                if (upCompletionHandler != null)
                {
                    upCompletionHandler(key, ResponseInfo.fileError(ex), null);
                }
            }
        }
예제 #19
0
 /// <summary>
 /// 上传文件流
 /// </summary>
 /// <param name="stream">文件流对象</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传结果处理器</param>
 #region 上传文件流
 public void uploadStream(Stream stream, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     long fileSize = stream.Length;
     if (fileSize <= Config.PUT_THRESHOLD)
     {
         new FormUploader().uploadStream(this.httpManager, stream, key, token, uploadOptions, upCompletionHandler);
     }
     else
     {
         string recorderKey = null;
         if (this.keyGenerator != null)
         {
             recorderKey = this.keyGenerator();
         }
         new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
     }
 }
예제 #20
0
        public ResumeUploader(ResumeRecorder recorder, string recordKey, Stream stream,
                              string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            this.mHttpManager        = new HttpManager();
            this.resumeRecorder      = recorder;
            this.recordKey           = recordKey;
            this.fileStream          = stream;
            this.key                 = key;
            this.uploadOptions       = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions;
            this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    this.uploadOptions.ProgressHandler(key, 1.0);
                }

                if (this.fileStream != null)
                {
                    try
                    {
                        this.fileStream.Close();
                        this.fileStream = null;
                    }
                    catch (Exception) { }
                }

                try
                {
                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("resumable upload completion error, {0}", ex.Message);
                }
            });
            string upTokenHeader = string.Format("UpToken {0}", token);

            this.upHeaders = new Dictionary <string, string>();
            this.upHeaders.Add("Authorization", upTokenHeader);
            this.chunkBuffer = new byte[Config.CHUNK_SIZE];
        }
예제 #21
0
        /// <summary>
        /// 上传文件流
        /// </summary>
        /// <param name="stream">文件流对象</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传结果处理器</param>
        #region   文件流
        public void uploadStream(Stream stream, string key, string token,
                                 UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            long fileSize = stream.Length;

            if (fileSize <= Config.PUT_THRESHOLD)
            {
                new FormUploader().uploadStream(stream, key, token, uploadOptions, upCompletionHandler);
            }
            else
            {
                string recorderKey = null;
                if (this.keyGenerator != null)
                {
                    recorderKey = this.keyGenerator();
                }
                new ResumeUploader(this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
            }
        }
예제 #22
0
        /// <summary>
        /// 构建分片上传对象
        /// </summary>
        /// <param name="recorder">分片上传进度记录器</param>
        /// <param name="recordKey">分片上传进度记录文件名</param>
        /// <param name="filePath">上传的文件全路径</param>
        /// <param name="key">保存在七牛的文件名</param>
        /// <param name="token">上传凭证</param>
        /// <param name="uploadOptions">上传可选设置</param>
        /// <param name="upCompletionHandler">上传完成结果处理器</param>
        public ResumeUploader(ResumeRecorder recorder, string recordKey, string filePath,
            string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            this.mHttpManager = new HttpManager();
            this.resumeRecorder = recorder;
            this.recordKey = recordKey;
            this.filePath = filePath;
            this.key = key;
            this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions;
            this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response)
            {
                if (respInfo.isOk())
                {
                    this.uploadOptions.ProgressHandler(key, 1.0);
                }

                if (this.fileStream != null)
                {
                    try
                    {
                        this.fileStream.Close();
                        this.fileStream = null;
                    }
                    catch (Exception) { }
                }

                try
                {
                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("resumable upload completion error, {0}", ex.Message);
                }
            });
            string upTokenHeader = string.Format("UpToken {0}", token);
            this.upHeaders = new Dictionary<string, string>();
            this.upHeaders.Add("Authorization", upTokenHeader);
            this.chunkBuffer = new byte[Config.CHUNK_SIZE];
        }
예제 #23
0
        public static void uploadBigFile()
        {
            Mac mac = new Mac(Settings.AccessKey, Settings.SecretKey);

            string bucket = "BUCKET";
            string saveKey = "SAVE_KEY";
            string localFile = "LOCAL_FILE";
            string recordPath = "RECORD_PATH";
            string recordFile = "RECORD_FILE";

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = bucket;
            putPolicy.SetExpires(3600);
            putPolicy.DeleteAfterDays = 1;

            string token = Auth.createUploadToken(putPolicy, mac);

            ResumeRecorder rr = new ResumeRecorder(recordPath);

            UploadOptions uploadOptions = new UploadOptions(
                null, // ExtraParams
                null, // MimeType
                false,  // CheckCrc32
                new UpProgressHandler(OnUploadProgressChanged), // 上传进度
                null // CancelSignal
                );

            UpCompletionHandler uploadCompleted = new UpCompletionHandler(OnUploadCompleted); // 上传完毕

            ResumeUploader ru = new ResumeUploader(
                rr,               // 续传记录
                recordFile,       // 续传记录文件
                localFile,        // 待上传的本地文件
                saveKey,          // 要保存的文件名
                token,            // 上传凭证
                uploadOptions,    // 上传选项(其中包含进度处理),可为null
                uploadCompleted   // 上传完毕事件处理
                );

            ru.uploadFile();
        }
예제 #24
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary <string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                case PostContentType.BYTES:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                    break;

                case PostContentType.STREAM:
                    long   streamLength = postArgs.Stream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                    postArgs.Stream.Seek(0, SeekOrigin.Begin);
                    break;

                case PostContentType.FILE:
                    postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                    break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
예제 #25
0
        private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            postArgs.Params = new Dictionary<string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                postArgs.Params.Add("key", key);
            }
            //设置token
            postArgs.Params.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (httpManager.FileContentType)
                {
                    case PostContentType.BYTES:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length)));
                        break;
                    case PostContentType.STREAM:
                        long streamLength = postArgs.Stream.Length;
                        byte[] buffer = new byte[streamLength];
                        int cnt = postArgs.Stream.Read(buffer, 0, (int)streamLength);
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt)));
                        postArgs.Stream.Seek(0, SeekOrigin.Begin);
                        break;
                    case PostContentType.FILE:
                        postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File)));
                        break;
                }
            }

            //设置MimeType
            postArgs.MimeType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams)
            {
                postArgs.Params.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            httpManager.CancellationSignal = new CancellationSignal(delegate()
            {
                return uploadOptions.CancellationSignal();
            });
            httpManager.PostArgs = postArgs;
            //第一次失败后使用备用域名重试一次
            httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                if (respInfo.needRetry())
                {
                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin);
                    }
                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        uploadOptions.ProgressHandler(key, 1.0);

                        if (httpManager.PostArgs.Stream != null)
                        {
                            httpManager.PostArgs.Stream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            upCompletionHandler(key, retryRespInfo, retryResponse);
                        }
                    });
                    httpManager.CompletionHandler = retried;
                    httpManager.multipartPost(Config.UP_HOST);
                }
                else
                {
                    uploadOptions.ProgressHandler(key, 1.0);

                    if (httpManager.PostArgs.Stream != null)
                    {
                        httpManager.PostArgs.Stream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        upCompletionHandler(key, respInfo, response);
                    }
                }
            });
            httpManager.multipartPost(Config.UPLOAD_HOST);
        }
예제 #26
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="httpManager">HttpManager对象</param>
 /// <param name="filePath">文件的完整路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadFile(string filePath, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromPath(key, null, filePath);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
예제 #27
0
 public void uploadData(byte[] data, int offset, int count, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromSlice(key, null, data, offset, count);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
예제 #28
0
 /// <summary>
 /// 上传字节数据
 /// </summary>
 /// <param name="data">二进制数据</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传结果处理器</param>
 #region   字节数据
 public void uploadData(byte[] data, string key,
                        string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     new FormUploader().uploadData(data, key, token, uploadOptions, upCompletionHandler);
 }
예제 #29
0
 /// <summary>
 /// 以表单方式上传数据流
 /// </summary>
 /// <param name="stream">文件数据流</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传完成结果处理器</param>
 public void uploadStream(Stream stream, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     HttpFormFile fFile = HttpFormFile.NewFileFromStream(key, null, stream);
     upload(fFile, key, token, uploadOptions, upCompletionHandler);
 }
예제 #30
0
        private void upload(HttpFormFile fFile, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            string uploadHost = "<UPLOAD_HOST>";
            string uploadHostRetry = "<UPLOAD_HOST_RETRY>";
            if(Config.UploadFromCDN)
            {
                uploadHost = Config.ZONE.UploadHost;
                uploadHostRetry = Config.ZONE.UpHost;
            }
            else
            {
                uploadHost = Config.ZONE.UpHost;
                uploadHostRetry = Config.ZONE.UploadHost;
            }

            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary<string, string> vPostParams = new Dictionary<string, string>();
            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                    case HttpFileType.DATA_SLICE:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                        break;
                    case HttpFileType.DATA_BYTES:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                        break;
                    case HttpFileType.FILE_STREAM:
                        long streamLength = fFile.BodyStream.Length;
                        byte[] buffer = new byte[streamLength];
                        int cnt = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                        break;
                    case HttpFileType.FILE_PATH:
                        vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                        break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate (long bytesWritten, long totalBytes)
             {
                 double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                 {
                     percent = 0.95;
                 }
                 uploadOptions.ProgressHandler(key, percent);
             });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate ()
             {
                 return uploadOptions.CancellationSignal();
             });

            // 第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}",respInfo.StatusCode);

                if (respInfo.needRetry()) // 需要重试
                {
                    Console.WriteLine(string.Format("form upload retry"));

                    if (Config.RetryWaitForNext)
                    {
                        Console.WriteLine(string.Format("wait for {0} milisecond(s)", Config.RETRY_INTERVAL_MILISEC));
                        System.Threading.Thread.Sleep(Config.RETRY_INTERVAL_MILISEC);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate (ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}",retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });

                    // 使用UPLOAD_HOST_RETRY重试
                    this.mHttpManager.postMultipartDataForm(uploadHostRetry, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else // 不需要重试
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            // 使用UPLOAD_HOST上传
            this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
        private void upload(HttpFormFile fFile, string key, string token,
                            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            if (uploadOptions == null)
            {
                uploadOptions = UploadOptions.defaultOptions();
            }
            Dictionary <string, string> vPostParams = new Dictionary <string, string>();

            //设置key
            if (!string.IsNullOrEmpty(key))
            {
                vPostParams.Add("key", key);
            }
            //设置token
            vPostParams.Add("token", token);
            //设置crc32校验
            if (uploadOptions.CheckCrc32)
            {
                switch (fFile.BodyType)
                {
                case HttpFileType.DATA_SLICE:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count)));
                    break;

                case HttpFileType.DATA_BYTES:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes)));
                    break;

                case HttpFileType.FILE_STREAM:
                    long   streamLength = fFile.BodyStream.Length;
                    byte[] buffer       = new byte[streamLength];
                    int    cnt          = fFile.BodyStream.Read(buffer, 0, (int)streamLength);
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt)));
                    fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    break;

                case HttpFileType.FILE_PATH:
                    vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile)));
                    break;
                }
            }

            //设置MimeType
            // FIX: (添加了下一行代码)
            // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题
            // @fengyh 2016-08-17 14:50
            fFile.ContentType = uploadOptions.MimeType;
            //设置扩展参数
            foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams)
            {
                vPostParams.Add(kvp.Key, kvp.Value);
            }
            //设置进度处理和取消信号
            ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes)
            {
                double percent = (double)bytesWritten / totalBytes;
                //这样做是为了等待回复
                if (percent > 0.95)
                {
                    percent = 0.95;
                }
                uploadOptions.ProgressHandler(key, percent);
            });

            CancellationSignal fCancelSignal = new CancellationSignal(delegate()
            {
                return(uploadOptions.CancellationSignal());
            });


            //第一次失败后使用备用域名重试一次
            CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response)
            {
                Console.WriteLine("form upload result, {0}", respInfo.StatusCode);
                if (respInfo.needRetry())
                {
                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Seek(0, SeekOrigin.Begin);
                    }

                    CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse)
                    {
                        Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode);
                        if (respInfo.isOk())
                        {
                            uploadOptions.ProgressHandler(key, 1.0);
                        }

                        if (fFile.BodyStream != null)
                        {
                            fFile.BodyStream.Close();
                        }

                        if (upCompletionHandler != null)
                        {
                            try
                            {
                                upCompletionHandler(key, retryRespInfo, retryResponse);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("form upload retry completion error, {0}", ex.Message);
                            }
                        }
                    });


                    this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, retried);
                }
                else
                {
                    if (respInfo.isOk())
                    {
                        uploadOptions.ProgressHandler(key, 1.0);
                    }

                    if (fFile.BodyStream != null)
                    {
                        fFile.BodyStream.Close();
                    }

                    if (upCompletionHandler != null)
                    {
                        try
                        {
                            upCompletionHandler(key, respInfo, response);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("form upload completion error, {0}", ex.Message);
                        }
                    }
                }
            });

            this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler);
        }
예제 #32
0
 /// <summary>
 /// 上传沙盒文件
 /// </summary>
 /// <param name="filePath">沙盒文件全路径</param>
 /// <param name="key">保存在七牛的文件名</param>
 /// <param name="token">上传凭证</param>
 /// <param name="uploadOptions">上传可选设置</param>
 /// <param name="upCompletionHandler">上传结果处理器</param>
 #region 上传沙盒文件
 public void uploadFile(string filePath, string key, string token,
     UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     try
     {
         long fileSize = 0;
         using (FileStream s = new FileStream(filePath, FileMode.Open, FileAccess.Read))
         {
             fileSize = s.Length;
         }
         //判断文件大小,选择上传方式
         if (fileSize <= Config.PUT_THRESHOLD)
         {
             new FormUploader().uploadFile(this.httpManager, filePath, key, token, uploadOptions, upCompletionHandler);
         }
         else
         {
             string recorderKey = null;
             if (this.keyGenerator != null)
             {
                 recorderKey = this.keyGenerator();
             }
             new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile();
         }
     }
     catch (Exception ex)
     {
         if (upCompletionHandler != null)
         {
             upCompletionHandler(key, ResponseInfo.fileError(ex), null);
         }
     }
 }
예제 #33
0
 public void uploadData(byte[] data, string key,
     string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
 {
     new FormUploader().uploadData(data, key, token, uploadOptions, upCompletionHandler);
 }
예제 #34
0
        public void uploadStream(Stream stream, string key, string token,
            UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler)
        {
            long fileSize = stream.Length;
            if (fileSize <= Config.PUT_THRESHOLD)
            {
                new FormUploader().uploadStream(stream, key, token, uploadOptions, upCompletionHandler);
            }
            else
            {
                if (this.resumeRecorder == null)
                {
                    string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                    this.resumeRecorder = new ResumeRecorder(home);
                }

                string recorderKey = null;

                if (this.keyGenerator == null)
                {
                    recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(key));
                }
                else
                {
                    recorderKey = this.keyGenerator();
                }

                new ResumeUploader(this.resumeRecorder, recorderKey, stream, key, token, uploadOptions, upCompletionHandler).uploadStream();
            }
        }