public async Task <string[]> OnUploadBeginAsync(DropZoneFile file) { if (file is null) { throw new ArgumentNullException(nameof(file)); } if (file.Path is null) { throw new ArgumentException("file's Path Property should not be null.", nameof(file)); } if (file.Name is null) { throw new ArgumentException("file's Name Property should not be null.", nameof(file)); } var args = new DropZoneUploadEventArgs(file.Path, file.Name, file.Size, file.Key, file.SessionId); await UploadStarted.InvokeAsync(args).ConfigureAwait(true); if (args.FormFields.Count == 0) { return(new string[0]); } else { var fields = new System.Collections.Generic.List <string>(); foreach (var kvp in args.FormFields) { fields.Add($"{kvp.Key}={kvp.Value}"); } return(fields.ToArray()); } }
protected virtual void OnUploadStarted(string fileName) { AutomationEventArgs args = new AutomationEventArgs(); args.FileName = fileName; args.Progress = 0; UploadStarted?.Invoke(args); }
/// <summary> /// 处理多文件上传 /// </summary> /// <param name="p_files"></param> async Task <bool> HandleUpload(IList <FileData> p_files) { UploadStarted?.Invoke(this, EventArgs.Empty); List <string> result = null; _cts?.Dispose(); _cts = new CancellationTokenSource(); try { result = await Uploader.Send(p_files, FixedVolume, _cts); } catch (Exception ex) { Log.Error(ex, "上传出错"); } finally { _cts?.Dispose(); } bool suc = false; if (result == null || result.Count != p_files.Count) { // 失败 if (_cts == null) { Kit.Msg("已取消上传!"); } else { Kit.Warn("😢上传失败,请重新上传!"); } foreach (var vf in p_files) { ((IUploadUI)vf.UploadUI).UploadFail(vf); } // 加载旧列表 ReadData(Data); } else { suc = true; for (int i = 0; i < p_files.Count; i++) { var vf = p_files[i]; await((IUploadUI)vf.UploadUI).UploadSuccess(result[i], vf); } WriteData(); } UploadFinished?.Invoke(this, suc); return(suc); }
/// <summary> /// Uploads a file /// </summary> /// <param name="filename">The filename we want to upload</param> /// <param name="MD5Checksum">The MD5 checksum of a file</param> /// <returns>An Image object that describes the image we uploaded</returns> public async Task UploadImageAsync(string filename, string MD5Checksum, CancellationToken token) { const string url = "https://upload.smugmug.com/"; try { var file = new FileInfo(filename); var myWebRequest = (HttpWebRequest)WebRequest.Create(url); myWebRequest.Method = WebRequestMethods.Http.Post; myWebRequest.ContentLength = file.Length; myWebRequest.UserAgent = "KHainePhotography PhotoStudioManager v1.0"; switch (file.Extension.ToLower()) { case "mp4": myWebRequest.ContentType = "video/mp4"; break; case "jpg": myWebRequest.ContentType = "image/jpeg"; break; default: myWebRequest.ContentType = "binary/octet-stream"; break; } // Add the authorization header myWebRequest.Headers.Add("Authorization", oAuthUtility.GetAuthorizationHeader(Album.Token, myWebRequest.RequestUri.AbsoluteUri)); myWebRequest.Headers.Add("Content-MD5", MD5Checksum); myWebRequest.Headers.Add("X-Smug-ResponseType", "JSON"); myWebRequest.Headers.Add("X-Smug-Version", "v2"); myWebRequest.Headers.Add("X-Smug-AlbumUri", Album.Uri); myWebRequest.Headers.Add("X-Smug-FileName", file.Name); myWebRequest.Headers.Add("X-Smug-Title", Path.GetFileNameWithoutExtension(file.FullName)); myWebRequest.Headers.Add("X-Smug-Caption", Path.GetFileNameWithoutExtension(file.FullName)); myWebRequest.Headers.Add("X-Smug-Latitude", "53.457920"); myWebRequest.Headers.Add("X-Smug-Longitude", "-1.464252"); myWebRequest.Headers.Add("X-Smug-Altitude", "86"); myWebRequest.Headers.Add("X-Smug-Pretty", "true"); //we start reading from the file... //we have some elements to set //- request time out (compute this for 10 kb/sec speed) //- the chunk size to use when uploading (how much data to report after) UploadStarted?.Invoke(this, new UploadEventArgs() { FileName = file.FullName, PercentComplete = 0 }); int timeOut = ((int)file.Length / 1024) * 1000; myWebRequest.Timeout = timeOut; myWebRequest.ReadWriteTimeout = timeOut; myWebRequest.AllowWriteStreamBuffering = true; long howMuchRead = 0; byte[] buffer = new byte[chunkSize]; int readSoFar = 0; using (FileStream sr = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) { using (var stream = await myWebRequest.GetRequestStreamAsync()) { while (howMuchRead < file.Length) { //we try to read a chunk from the file readSoFar = sr.Read(buffer, 0, chunkSize); howMuchRead += readSoFar; //we now write those files to the web. await stream.WriteAsync(buffer, 0, readSoFar, token); UploadProgress?.Invoke(this, new UploadEventArgs() { FileName = file.FullName, PercentComplete = (float)howMuchRead / (float)file.Length }); } } } var resp = await myWebRequest.GetResponseAsync(); string rez = string.Empty; using (StreamReader ns = new StreamReader(resp.GetResponseStream())) { rez = await ns.ReadToEndAsync(); } UploadCompleted?.Invoke(this, new UploadEventArgs() { FileName = file.FullName, PercentComplete = (float)howMuchRead / (float)file.Length }); //we deserialize the image var response = JsonConvert.DeserializeObject <UploadReponse>(System.Uri.UnescapeDataString(rez)); if (response.stat != "ok") { throw new ApplicationException(response.stat); } } catch (Exception e) { Debug.Fail(e.Message); throw; } }