/// <summary> /// 下载远程图片 /// </summary> /// <param name="imageUrl">将要下载的图片地址</param> /// <param name="microblogId">微博Id</param> private void DownloadRemoteImage(string imageUrl, long microblogId) { if (UserContext.CurrentUser == null || microblogId <= 0 || string.IsNullOrEmpty(imageUrl)) { return; } try { WebRequest webRequest = WebRequest.Create(SiteUrls.FullUrl(imageUrl)); HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); Stream stream = httpWebResponse.GetResponseStream(); MemoryStream ms = new MemoryStream(); stream.CopyTo(ms); string friendlyFileName = imageUrl.Substring(imageUrl.LastIndexOf("/") + 1); string contentType = MimeTypeConfiguration.GetMimeType(friendlyFileName); bool isImage = contentType.StartsWith("image"); if (!isImage || stream == null || !stream.CanRead) { return; } Attachment attachment = new Attachment(ms, contentType, friendlyFileName); attachment.FileLength = httpWebResponse.ContentLength; attachment.AssociateId = microblogId; attachment.UserId = UserContext.CurrentUser.UserId; attachment.UserDisplayName = UserContext.CurrentUser.DisplayName; attachment.TenantTypeId = TenantTypeIds.Instance().Microblog(); var attachmentService = new AttachmentService(TenantTypeIds.Instance().Microblog()); attachmentService.Create(attachment, ms); ms.Dispose(); ms.Close(); } catch { } }
public ICentralizedFile AddUpdateFile(string path, string fileName, Stream contentStream) { if (!CentralizedFileStorage.IsValid(this.FileStoreKey, path, fileName)) throw this.CreateFilePathInvalidException(path, fileName); bool processEvents = EventExecutor != null && fileName != FileSystemFileStorageProvider.PLACE_HOLDER_FILENAME; ICentralizedFile originalFile = (ICentralizedFile)null; if (processEvents) { originalFile = GetFile(path, fileName); if (originalFile != null) EventExecutor.OnBeforeUpdate(originalFile); else EventExecutor.OnBeforeCreate(FileStoreKey, path, fileName); } this.GetConnection().Put(this._bucketName, this.MakeKey(path, fileName), new SortedList<string, string>(), contentStream, new SortedList<string, string>() { { "Content-Type", MimeTypeConfiguration.GetMimeType(fileName) } }); this.RemoveAmazonS3PathQueryResultsAndAmazonS3FileStorageFileFromCache(path, fileName); var file = GetFile(path, fileName); if (processEvents) { if (originalFile != null) EventExecutor.OnAfterUpdate(file); else EventExecutor.OnAfterCreate(file); } return file; }
public ActionResult UploadContentAttachment() { IUser user = UserContext.CurrentUser; if (user == null) { return(new EmptyResult()); } ContentAttachmentService attachementService = new ContentAttachmentService(); long userId = user.UserId; string userDisplayName = user.DisplayName; long attachmentId = 0; if (Request.Files.Count > 0 && Request.Files["Filedata"] != null) { HttpPostedFileBase postFile = Request.Files["Filedata"]; string fileName = postFile.FileName; string contentType = MimeTypeConfiguration.GetMimeType(postFile.FileName); ContentAttachment attachment = new ContentAttachment(postFile, contentType); attachment.UserId = userId; attachment.UserDisplayName = userDisplayName; using (Stream stream = postFile.InputStream) { attachementService.Create(attachment, stream); } attachmentId = attachment.AttachmentId; } return(Json(new { AttachmentId = attachmentId })); }
public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (MUploadConfig.MBase64) { uploadFileName = MUploadConfig.MBase64Filename; uploadFileBytes = Convert.FromBase64String(Request[MUploadConfig.MUploadFieldName]); } else { var file = Request.Files[MUploadConfig.MUploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.MState = MUploadState.MTypeNotAllow; WriteResult(); return; } if (!CheckFileSize(file.ContentLength)) { Result.MState = MUploadState.MSizeLimitExceed; WriteResult(); return; } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.MState = MUploadState.MNetworkError; WriteResult(); } } IUser user = UserContext.CurrentUser; string tenantTypeId = Request["tenantTypeId"]; bool resize = true; long associateId = 0; long.TryParse(Context.Request["associateId"], out associateId); AttachmentService <Attachment> attachementService = new AttachmentService <Attachment>(tenantTypeId); long userId = user.UserId, ownerId = user.UserId; string userDisplayName = user.DisplayName; Result.MOriginFileName = uploadFileName; try { //保存文件 string contentType = MimeTypeConfiguration.GetMimeType(uploadFileName); MemoryStream ms = new MemoryStream(uploadFileBytes); string friendlyFileName = uploadFileName.Substring(uploadFileName.LastIndexOf("\\") + 1); Attachment attachment = new Attachment(ms, contentType, friendlyFileName); attachment.UserId = userId; attachment.AssociateId = associateId; attachment.TenantTypeId = tenantTypeId; attachment.OwnerId = ownerId; attachment.UserDisplayName = userDisplayName; attachementService.Create(attachment, ms); ms.Dispose(); Result.MUrl = SiteUrls.Instance().AttachmentDirectUrl(attachment); Result.MState = MUploadState.MSuccess; } catch (Exception e) { Result.MState = MUploadState.MFileAccessError; Result.MErrorMessage = e.Message; } finally { WriteResult(); } }