public Crawler Fetch() { if (!IsExternalIPAddress(this.SourceUrl)) { State = "INVALID_URL"; return(this); } var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest; 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), Config.GetString("catcherPathFormat")); var savePath = IO.Path.WebRootPath() + ServerUrl; if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } try { var stream = response.GetResponseStream(); var reader = new BinaryReader(stream); byte[] bytes; using (var ms = new MemoryStream()) { byte[] buffer = new byte[4096]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { ms.Write(buffer, 0, count); } bytes = ms.ToArray(); } File.WriteAllBytes(savePath, bytes); bool upRemote = false; if (upRemote) { //将文件转存至 oss 并清理本地文件 var oss = new Common.AliYun.OssHelper(); var upload = oss.FileUpload(savePath, "Files/" + DateTime.Now.ToString("yyyy/MM/dd"), Path.GetFileName(this.SourceUrl)); if (upload) { Common.IO.IOHelper.DeleteFile(savePath); ServerUrl = "Files/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + Path.GetFileName(savePath); State = "SUCCESS"; } else { State = "抓取错误:阿里云OSS转存失败"; } } else { State = "SUCCESS"; } } catch (Exception e) { State = "抓取错误:" + e.Message; } return(this); } }
public bool UploadFile(string business, Guid key, string sign) { try { var userId = Guid.Parse(HttpContext.Session.GetString("userId")); var ReqFiles = Request.Form.Files; List <IFormFile> Attachments = new List <IFormFile>(); for (int i = 0; i < ReqFiles.Count; i++) { Attachments.Add(ReqFiles[i]); } var url = string.Empty; var fileName = string.Empty; var fileExtension = string.Empty; var fullFileName = string.Empty; string filepath = Libraries.IO.Path.WebRootPath() + "/Upload/" + DateTime.Now.ToString("yyyy/MM/dd"); foreach (var file in Attachments) { if (file != null) { Directory.CreateDirectory(filepath); fileName = Guid.NewGuid().ToString(); fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant(); fullFileName = string.Format("{0}{1}", fileName, fileExtension); if (file != null && file.Length > 0) { string path = filepath + "/" + fullFileName; using (FileStream fs = System.IO.File.Create(path)) { file.CopyTo(fs); fs.Flush(); } var upRemote = false; if (upRemote) { var oss = new Common.AliYun.OssHelper(); var upload = oss.FileUpload(path, "Files/" + DateTime.Now.ToString("yyyy/MM/dd"), file.FileName); if (upload) { Common.IO.IOHelper.DeleteFile(path); path = "/Files/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + fullFileName; } } else { path = path.Replace(Libraries.IO.Path.WebRootPath(), ""); } TFile fi = new TFile(); fi.Id = Guid.NewGuid(); fi.CreateUserId = userId; fi.CreateTime = DateTime.Now; fi.IsDelete = false; fi.Name = file.FileName; fi.Table = business; fi.TableId = key; fi.Sign = sign; fi.Path = path; db.TFile.Add(fi); db.SaveChanges(); } } } return(true); } catch { return(false); } }
public override string Process() { string value = ""; byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Http.HttpContext.Current().Request.Form[UploadConfig.UploadFieldName]); var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); var localPath = IO.Path.WebRootPath() + savePath; try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); bool upRemote = false; if (upRemote) { //将文件转存至 oss 并清理本地文件 var oss = new Common.AliYun.OssHelper(); var upload = oss.FileUpload(localPath, "Files/" + DateTime.Now.ToString("yyyy/MM/dd"), Path.GetFileName(localPath)); if (upload) { Common.IO.IOHelper.DeleteFile(localPath); Result.Url = "Files/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + Path.GetFileName(localPath); Result.State = UploadState.Success; } else { Result.State = UploadState.FileAccessError; Result.ErrorMessage = "阿里云OSS文件转存失败"; } } else { Result.Url = savePath; Result.State = UploadState.Success; } } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { value = WriteResult(); } } else { var file = Http.HttpContext.Current().Request.Form.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; value = WriteResult(); } int filelength = Convert.ToInt32(file.Length); if (!CheckFileSize(filelength)) { Result.State = UploadState.SizeLimitExceed; value = WriteResult(); } uploadFileBytes = new byte[file.Length]; try { file.OpenReadStream(); var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); var localPath = IO.Path.WebRootPath() + savePath; try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } using (FileStream fs = System.IO.File.Create(localPath)) { file.CopyTo(fs); fs.Flush(); } bool upRemote = false; if (upRemote) { //将文件转存至 oss 并清理本地文件 var oss = new Common.AliYun.OssHelper(); var upload = oss.FileUpload(localPath, "Files/" + DateTime.Now.ToString("yyyy/MM/dd"), file.FileName); if (upload) { Common.IO.IOHelper.DeleteFile(localPath); Result.Url = "Files/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + Path.GetFileName(localPath); Result.State = UploadState.Success; } else { Result.State = UploadState.FileAccessError; Result.ErrorMessage = "阿里云OSS文件转存失败"; } } else { Result.Url = savePath; Result.State = UploadState.Success; } } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { value = WriteResult(); } } catch (Exception) { Result.State = UploadState.NetworkError; WriteResult(); } } return(value); }