Пример #1
0
        /// <summary>
        /// Saves the file to LeanCloud.
        /// </summary>
        /// <param name="onProgress"></param>
        /// <returns></returns>
        public async Task <LCFile> Save(Action <long, long> onProgress = null)
        {
            if (!string.IsNullOrEmpty(Url))
            {
                // 外链方式
                await base.Save();
            }
            else
            {
                // 上传文件
                Dictionary <string, object> uploadToken = await GetUploadToken();

                string uploadUrl = uploadToken["upload_url"] as string;
                string key       = uploadToken["key"] as string;
                string token     = uploadToken["token"] as string;
                string provider  = uploadToken["provider"] as string;
                try {
                    if (provider == "s3")
                    {
                        // AWS
                        LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, stream);
                        await uploader.Upload(onProgress);
                    }
                    else if (provider == "qiniu")
                    {
                        // Qiniu
                        LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, stream);
                        await uploader.Upload(onProgress);
                    }
                    else
                    {
                        throw new Exception($"{provider} is not support.");
                    }
                    LCObjectData objectData = LCObjectData.Decode(uploadToken);
                    Merge(objectData);
                    _ = LCCore.HttpClient.Post <Dictionary <string, object> >("fileCallback", data: new Dictionary <string, object> {
                        { "result", true },
                        { "token", token }
                    });
                } catch (Exception e) {
                    _ = LCCore.HttpClient.Post <Dictionary <string, object> >("fileCallback", data: new Dictionary <string, object> {
                        { "result", false },
                        { "token", token }
                    });
                    throw e;
                }
            }
            return(this);
        }
Пример #2
0
        public async Task <LCFile> Save(Action <long, long> onProgress = null)
        {
            if (!string.IsNullOrEmpty(Url))
            {
                // 外链方式
                await base.Save();
            }
            else
            {
                // 上传文件
                Dictionary <string, object> uploadToken = await GetUploadToken();

                string uploadUrl = uploadToken["upload_url"] as string;
                string key       = uploadToken["key"] as string;
                string token     = uploadToken["token"] as string;
                string provider  = uploadToken["provider"] as string;
                if (provider == "s3")
                {
                    // AWS
                    LCAWSUploader uploader = new LCAWSUploader(uploadUrl, MimeType, data);
                    await uploader.Upload(onProgress);
                }
                else if (provider == "qiniu")
                {
                    // Qiniu
                    LCQiniuUploader uploader = new LCQiniuUploader(uploadUrl, token, key, data);
                    await uploader.Upload(onProgress);
                }
                else
                {
                    throw new Exception($"{provider} is not support.");
                }
                LCObjectData objectData = LCObjectData.Decode(uploadToken);
                Merge(objectData);
            }
            return(this);
        }