public async Task Process(UploadConfig uploadConfig) { this._uploadConfig = uploadConfig; byte[] uploadFileBytes = null; string uploadFileName = null; if (_uploadConfig.Base64) { uploadFileName = _uploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(_accessor.HttpContext.Request.Query[_uploadConfig.UploadFieldName]);//注意可能会错 } else { var file = _accessor.HttpContext.Request.Form.Files[_uploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { this._result.State = UploadState.TypeNotAllow; await WriteResult(); return; } if (!CheckFileSize(file.Length)) { this._result.State = UploadState.SizeLimitExceed; await WriteResult(); return; } //uploadFileBytes = new byte[file.Length]; try { var memoryStream = new MemoryStream(); await file.CopyToAsync(memoryStream); if (_uploadConfig.ActionName == "uploadimage") { _pictureHelper.ProcessByStream(memoryStream, new PictureSize { Width = 700, Height = 700, Mode = "Auto" }); memoryStream.Dispose(); memoryStream = _pictureHelper.Ms; } uploadFileBytes = new byte[memoryStream.Length]; memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Read(uploadFileBytes, 0, uploadFileBytes.Length); memoryStream.Dispose(); //file.InputStream.Read(uploadFileBytes, 0, file.Length); //file.OpenReadStream().Read(uploadFileBytes, 0, uploadFileBytes.Length); } catch (Exception) { _result.State = UploadState.NetworkError; await WriteResult(); } } _result.OriginFileName = uploadFileName; var savePath = PathFormatter.Format(uploadFileName, _uploadConfig.PathFormat); string extension = Path.GetExtension(uploadFileName); string filename = Path.GetFileName(savePath); var localPath = Path.Combine(_hostingEnvironment.WebRootPath, savePath); try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } //File.WriteAllBytes(localPath, uploadFileBytes); await File.WriteAllBytesAsync(localPath, uploadFileBytes); await _uploadService.SaveToRemotePath(uploadFileBytes, savePath);//保存到远程路径 await _uploadService.addFile(filename, extension, "Ueditor"); _result.Url = savePath; _result.State = UploadState.Success; } catch (Exception e) { _result.State = UploadState.FileAccessError; _result.ErrorMessage = e.Message; } finally { await WriteResult(); } }
public async Task <Crawler> Fetch(string sourceUrl) { this.sourceUrl = sourceUrl; if (!IsExternalIPAddress(this.sourceUrl)) { state = "INVALID_URL"; return(this); } var request = WebRequest.Create(this.sourceUrl) as WebRequest; using (var response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { state = "Url returns " + response.StatusCode + ", " + response.StatusDescription; return(this); } if (response.ContentType.IndexOf("image") == -1) { state = "Url is not an image"; return(this); } serverUrl = PathFormatter.Format(Path.GetFileName(this.sourceUrl), _editorConfig.GetString("catcherPathFormat")); //var savePath = Server.MapPath(ServerUrl); var savePath = Path.Combine(_hostingEnvironment.WebRootPath, serverUrl); string extension = Path.GetExtension(serverUrl); string filename = Path.GetFileName(serverUrl); if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } try { byte[] uploadFileBytes = null; WebClient webClient = new WebClient(); webClient.Credentials = CredentialCache.DefaultCredentials; //var memoryStream = await webClient.OpenWriteTaskAsync(this.sourceUrl); byte[] bytes = await webClient.DownloadDataTaskAsync(this.sourceUrl); var memoryStream = new MemoryStream(bytes); webClient.Dispose(); _pictureHelper.ProcessByStream(memoryStream, new PictureSize { Width = 700, Height = 700, Mode = "Auto" }); memoryStream.Dispose(); memoryStream = _pictureHelper.Ms; uploadFileBytes = new byte[memoryStream.Length]; memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.Read(uploadFileBytes, 0, uploadFileBytes.Length); memoryStream.Dispose(); await File.WriteAllBytesAsync(savePath, uploadFileBytes); await _uploadService.SaveToRemotePath(uploadFileBytes, serverUrl);//保存到远程路径 await _uploadService.addFile(filename, extension, "Ueditor"); state = "SUCCESS"; } catch (Exception e) { state = "抓取错误:" + e.Message; } return(this); } }