private ResourceEntity InnerSave(int userid, bool isdefault, FileInfor fileInfor, int sortOrder, int sourceId, SourceType sourceType) { //存储到数据库 var entity = new ResourceEntity { CreatedDate = DateTime.Now, CreatedUser = userid, IsDefault = isdefault, Name = fileInfor.FileName, //+ "." + fileInfor.FileExtName,//这里要注意 ExtName = fileInfor.FileExtName, Width = fileInfor.Width, Height = fileInfor.Height, ContentSize = fileInfor.FileSize, Size = fileInfor.Width.ToString(CultureInfo.InvariantCulture) + "x" + fileInfor.Height.ToString(CultureInfo.InvariantCulture), SortOrder = sortOrder, SourceId = sourceId, SourceType = (int)sourceType, Status = (int)DataStatus.Normal, Type = (int)fileInfor.ResourceType, UpdatedDate = DateTime.Now, UpdatedUser = userid, Domain = String.Empty }; return(entity); }
/// <summary> /// restricted to upload image file , for staging purpose /// </summary> /// <param name="file"></param> /// <param name="key"></param> /// <param name="fileInfor"></param> /// <param name="p2"></param> /// <returns></returns> internal static FileMessage UploadFile(FileInfo file, string key, out FileInfor fileInfor, string p2) { var client = _imageService; string fileKey; string fileExt; fileInfor = new FileInfor(); FileMessage f = client.GetFileName(key, (int)file.Length, file.Name, file.Extension, out fileKey, out fileExt); if (f == FileMessage.Success) { fileInfor.FileName = fileKey; fileInfor.FileSize = (int)file.Length; fileInfor.FileExtName = fileExt; fileInfor.ResourceType = ContentType.GetResourceType(fileExt); try { FileUploadMessage inValue = new FileUploadMessage(); inValue.FileName = fileKey; inValue.KeyName = key; inValue.FileExt = fileExt; inValue.SaveOrigin = true; using (var fileHR = file.OpenRead()) { inValue.FileData = fileHR; int width, height; using (var image = System.Drawing.Image.FromStream(fileHR)) { width = image.Width; height = image.Height; } fileHR.Seek(0, SeekOrigin.Begin); fileInfor.Width = width; fileInfor.Height = height; client.UploadFileAndReturnInfo(inValue); fileHR.Close(); } return(FileMessage.Success); } catch { return(FileMessage.UnknowError); } } else { return(f); } }
/// <summary> /// 上传文件并返回缩略图大小a /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(HttpPostedFileBase postedFile, string key, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo) { ImageServiceClientProxy client = new ImageServiceClientProxy(); string fileKey; string fileExt; fileInfor = new FileInfor(); thumbnailInfo = new ThumbnailInfo(); FileMessage f = client.GetFileName(key, postedFile.ContentLength, postedFile.FileName, out fileKey, out fileExt); if (f == FileMessage.Success) { try { fileInfor.FileName = fileKey; fileInfor.FileSize = postedFile.ContentLength; fileInfor.FileExtName = fileExt; FileUploadMessage inValue = new FileUploadMessage(); inValue.FileName = fileKey; inValue.KeyName = key; inValue.FileExt = fileExt; inValue.SaveOrigin = true; if (postedFile.InputStream.CanRead) { //byte[] content = new byte[postedFile.ContentLength + 1]; //postedFile.InputStream.Read(content, 0, postedFile.ContentLength); //inValue.FileData = new MemoryStream(content); inValue.FileData = postedFile.InputStream; thumbnailInfo = client.UploadFileAndReturnInfo(inValue); inValue.FileData.Close(); inValue.FileData.Dispose(); } else { return(FileMessage.UnknowError); } return(FileMessage.Success); } catch { return(FileMessage.UnknowError); } } else { return(f); } }
/// <summary> /// 抓取网络图片 /// </summary> /// <param name="key">服务端配置索引名</param> /// <param name="originalImageUrl">原图片的Url</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <returns></returns> public static FileMessage SaveWebImage(string key, string originalImageUrl, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo) { var req = WebRequest.Create(originalImageUrl); var resp = req.GetResponse(); using (var stream = resp.GetResponseStream()) { //var imageFileName = originalImageUrl.Substring(originalImageUrl.LastIndexOf("/") + 1) + ".jpg"; //Note:参数fileName仅用于截取扩展名后判断“是否允许上传该类型图片”,硬编码为“image.jpg”以减少不必要的字符串操作 return(UploadFileAndReturnInfo(key, stream, (int)resp.ContentLength, "image.jpg", out fileInfor, out thumbnailInfo)); } }
/// <summary> /// 上传文件并返回缩略图大小 /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(string key, Stream inputStream, int contentLength, string fileName, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo) { ImageServiceClientProxy client = new ImageServiceClientProxy(); string fileKey; string fileExt; fileInfor = new FileInfor(); thumbnailInfo = new ThumbnailInfo(); FileMessage f = client.GetFileName(key, contentLength, fileName, out fileKey, out fileExt); if (f == FileMessage.Success) { try { fileInfor.FileName = fileKey; fileInfor.FileSize = contentLength; FileUploadMessage inValue = new FileUploadMessage(); inValue.FileName = fileKey; inValue.KeyName = key; inValue.FileExt = fileExt; inValue.SaveOrigin = true; if (inputStream.CanRead) { inValue.FileData = inputStream; thumbnailInfo = client.UploadFileAndReturnInfo(inValue); inValue.FileData.Close(); inValue.FileData.Dispose(); } else { return(FileMessage.UnknowError); } return(FileMessage.Success); } catch { return(FileMessage.UnknowError); } } else { return(f); } }
private IEnumerable <ImageUploadInfo> TransferImageToStage() { FileInfo imageFile = new FileInfo(_filePath); int jobId = _context.JobId; FileInfor fileInfor = _resourceService.SaveStage(imageFile, 4, SourceType.Product); if (fileInfor != null) { var itemNames = Path.GetFileNameWithoutExtension(_filePath).Split('@'); int sortOrder = 1; int.TryParse(itemNames.Length > 1 ? itemNames[1] : "1", out sortOrder); var entity = _productRepService.Entry <ResourceStageEntity>(); entity.ContentSize = fileInfor.FileSize; entity.ExtName = fileInfor.FileExtName; entity.Name = fileInfor.FileName; entity.Width = fileInfor.Width; entity.Height = fileInfor.Height; entity.Size = fileInfor.Width.ToString(CultureInfo.InvariantCulture) + "x" + fileInfor.Height.ToString(CultureInfo.InvariantCulture); entity.SortOrder = sortOrder; entity.ItemCode = itemNames[0]; entity.InUser = _context.CurrentUser.CustomerId; entity.InDate = DateTime.Now; entity.UploadGroupId = jobId; _productRepService.Insert <ResourceStageEntity>(entity); DeleteTempFile(); yield return(new ImageUploadInfo() { ItemCode = entity.ItemCode , fileSize = entity.Size , FileName = entity.Name , Width = entity.Width , Height = entity.Height }); } else { yield break; } }
private IEnumerable <ImageUploadInfo> TransferImageToStage() { var image = _dbContext.ResourceStages.Create(); FileInfo imageFile = new FileInfo(_filePath); int jobId = _context.JobId; FileInfor fileInfor = _resourceService.SaveStage(imageFile, 4, SourceType.Product); if (fileInfor != null) { var entity = _dbContext.ResourceStages.Create(); entity.ContentSize = fileInfor.FileSize; entity.ExtName = fileInfor.FileExtName; entity.Name = fileInfor.FileName; entity.Width = fileInfor.Width; entity.Height = fileInfor.Height; entity.Size = fileInfor.Width.ToString(CultureInfo.InvariantCulture) + "x" + fileInfor.Height.ToString(CultureInfo.InvariantCulture); entity.SortOrder = 1; entity.ItemCode = Path.GetFileNameWithoutExtension(_filePath); entity.InUser = 4; entity.InDate = DateTime.Now; entity.UploadGroupId = jobId; _dbContext.ResourceStages.Add(entity); _dbContext.SaveChanges(); yield return(new ImageUploadInfo() { ItemCode = entity.ItemCode , fileSize = entity.Size , FileName = entity.Name , Width = entity.Width , Height = entity.Height }); } else { yield return(null); } }
/// <summary> /// Gets the file data. /// </summary> /// <returns> /// return null if not contain file... /// </returns> /// <param name='fileName'> /// File name. /// </param> public byte[] getFileData(string fileName) { FileInfor fileInfor = null; bool b = m_Files.ContainsKey(fileName); b = m_Files.TryGetValue(fileName, out fileInfor); if (b == false) { return(null); } FileStream fs; fs = File.Open(m_FileName, FileMode.Open, FileAccess.Read); if (fs == null || fs.CanRead == false) { Helper.LogError("getFileData file open error "); return(null); } byte[] data = new byte[fileInfor.m_Size]; try { fs.Seek(fileInfor.m_StartPos, SeekOrigin.Begin); fs.Read(data, 0, fileInfor.m_Size); fs.Close(); fs.Dispose(); } catch (System.Exception e) { Debug.LogException(e); fs.Close(); fs.Dispose(); return(null); } return(data); }
public static async Task <FileInfor> getFileInfor(string pathFile, string userToken) { var parameters = new Dictionary <string, string>(); parameters[""] = pathFile; var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", userToken); HttpResponseMessage response = await client.PostAsync(api + "/api/file/GetFileInfor/", new FormUrlEncodedContent(parameters)); string data = await response.Content.ReadAsStringAsync(); Log("CallAPIForOfficeAddin Line 49: getFileInfor: " + Environment.NewLine + "pathFile: " + pathFile + Environment.NewLine + "userToken: " + userToken + Environment.NewLine + data); FileInfor result; try { result = JsonConvert.DeserializeObject <FileInfor>(data); } catch { result = new FileInfor(); if ((string.Compare(data, "Không tìm thấy file này", true) == 0) || (string.Compare(data, "Path rỗng.", true) == 0)) { result.Id = 0; result.CanEdit = true; result.CheckoutBy = ""; } } client.Dispose(); return(result); }
/// <summary> /// 上传文件并返回缩略图大小( base64string ) /// </summary> /// <param name="key"></param> /// <param name="inputBase64String"></param> /// <param name="contentLength"></param> /// <param name="fileName"></param> /// <param name="fileInfor"></param> /// <param name="thumbnailInfo"></param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(string key, string inputBase64String, int contentLength, string fileName, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo) { char[] charBuffer = inputBase64String.ToCharArray(); byte[] bytes = Convert.FromBase64CharArray(charBuffer, 0, charBuffer.Length); using (var str = new MemoryStream(bytes)) { return(UploadFileAndReturnInfo(key, str, contentLength, fileName, out fileInfor, out thumbnailInfo)); } }
/// <summary> /// 上传文件并返回缩略图大小 /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(HttpPostedFile postedFile, string key, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo) { HttpPostedFileBase obj = new HttpPostedFileWrapper(postedFile); return(UploadFileAndReturnInfo(obj, key, out fileInfor, out thumbnailInfo)); }
/// <summary> /// 文件上传 /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <returns></returns> public static FileMessage UploadFile(HttpPostedFile postedFile, string key, out FileInfor fileInfor) { HttpPostedFileBase obj = new HttpPostedFileWrapper(postedFile); return(UploadFile(obj, key, out fileInfor)); }
/// <summary> /// 文件上传 /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <returns></returns> public static FileMessage UploadFile(HttpPostedFileBase postedFile, string key, out FileInfor fileInfor) { return(UploadFile(postedFile, key, out fileInfor, new string[0])); }
public Resource Sync(string channelProductId, string channelColorId, string channelUrl, string Id, int SeqNo, DateTime WriteTime) { var productMap = _channelMapper.GetMapByChannelValue(channelProductId, ChannelMapType.ProductId); if (productMap == null) { Log.InfoFormat("Failed to sync image:product unavailable,productId:[{0}],colorId:[{1}],url:[{2}]", channelProductId, channelColorId, channelUrl); return(null); } var mapKey = Utils.GetProductProprtyMapKey(productMap.LocalId, channelColorId); var colorIdMap = _channelMapper.GetMapByChannelValue(mapKey, ChannelMapType.ColorId); if (colorIdMap == null) { Log.InfoFormat("Failed to sync image, iamge's color has not sync,productId:[{0}],colorId:[{1}],url:[{2}]", channelProductId, channelColorId, channelUrl); return(null); } string filePath; FileInfor uploadFile = null; Resource resource = null; using (var db = new YintaiHZhouContext()) { //查找是否已经同步图片 var resourceExt = db.Resources.FirstOrDefault(r => r.ColorId == colorIdMap.LocalId && r.SourceId == productMap.LocalId && r.SortOrder == SeqNo && r.SourceType == 1); var product = db.Products.FirstOrDefault(x => x.Id == productMap.LocalId); if (product == null) { Log.InfoFormat("Failed to sync image,product is null"); return(null); } // 下载远程图片 if (resourceExt == null || resourceExt.UpdatedDate != WriteTime) { try { filePath = FetchRemotePic(channelUrl); } catch (Exception ex) { Log.ErrorFormat("下载图片出错,productId:[{0}],colorId:[{1}],url:[{2}]", channelProductId, channelColorId, channelUrl); Log.Error(ex); return(null); } //resize pics var file = new FileInfo(filePath); try { var uploadResult = FileUploadServiceManager.UploadFile(file, "product", out uploadFile, string.Empty); File.Delete(filePath); if (uploadResult != FileMessage.Success) { Log.ErrorFormat("上传文件失败:{0}", filePath); return(null); } } catch (Exception ex) { Log.Error(ex); return(null); } } if (uploadFile == null) { Log.ErrorFormat("Uploadfile is null"); return(null); } if (resourceExt == null) { var newResource = new Resource() { ColorId = colorIdMap.LocalId, SourceId = productMap.LocalId, SourceType = 1, ContentSize = uploadFile.FileSize, Domain = string.Empty, ExtName = uploadFile.FileExtName, Height = uploadFile.Height, IsDefault = false, Name = uploadFile.FileName, Status = 1, SortOrder = SeqNo, Size = string.Format("{0}x{1}", uploadFile.Width, uploadFile.Height), Type = (int)uploadFile.ResourceType, Width = uploadFile.Width, ChannelPicId = 0, CreatedDate = DateTime.Now, CreatedUser = SystemDefine.SystemUser, UpdatedUser = SystemDefine.SystemUser, UpdatedDate = WriteTime, }; db.Resources.Add(newResource); resource = newResource; } else { resourceExt.ContentSize = uploadFile.FileSize; resourceExt.UpdatedUser = SystemDefine.SystemUser; resourceExt.UpdatedDate = WriteTime; resourceExt.ExtName = uploadFile.FileExtName; resourceExt.Height = uploadFile.Height; resourceExt.IsDefault = false; resourceExt.Name = uploadFile.FileName; resourceExt.Size = string.Format("{0}x{1}", uploadFile.Width, uploadFile.Height); resourceExt.Type = (int)uploadFile.ResourceType; resourceExt.Width = uploadFile.Width; resource = resourceExt; } if (!product.IsHasImage) { product.IsHasImage = true; product.UpdatedDate = DateTime.Now; } db.SaveChanges(); return(resource); } }
/// <summary> /// Loads the pak file. /// </summary> /// <returns> /// return true if success else return false; /// </returns> /// <param name='filename'> /// If set to <c>true</c> filename. /// </param> public bool loadPakFile(string filename) { if (m_Files == null) { m_Files = new Dictionary <string, FileInfor> (); } m_FileName = filename; if (File.Exists(filename) == false) { Helper.LogError(" file is not exist"); return(false); } else { Helper.Log("file find..."); } m_Files.Clear(); FileStream fs; fs = File.Open(filename, FileMode.Open, FileAccess.Read); if (fs == null) { Helper.LogError("File.Open Failed "); return(false); } else { Helper.Log("File.Open success"); } if (fs.CanRead) { Helper.Log("fs can read "); } else { Helper.LogError("fs can not read"); } byte[] bytecount = new byte[300]; fs.Read(bytecount, 0, 4); int fileCount1 = System.BitConverter.ToInt32(bytecount, 0); Helper.Log("read file count:" + fileCount1); for (int i = 0; i < fileCount1; ++i) { FileInfor pTemFile = new FileInfor(); fs.Read(bytecount, 0, 4); pTemFile.m_id = System.BitConverter.ToInt32(bytecount, 0); fs.Read(bytecount, 0, 4); pTemFile.m_StartPos = System.BitConverter.ToInt32(bytecount, 0); fs.Read(bytecount, 0, 4); pTemFile.m_Size = System.BitConverter.ToInt32(bytecount, 0); fs.Read(bytecount, 0, 256); pTemFile.m_Path = System.Text.Encoding.Default.GetString(bytecount); int leng = pTemFile.m_Path.Length; for (int j = 0; j < leng; ++j) { char temchar = pTemFile.m_Path [j]; if (temchar == 0) { pTemFile.m_Path = pTemFile.m_Path.Substring(0, j); break; } } if (m_Files.ContainsKey(pTemFile.m_Path) == false) { m_Files.Add(pTemFile.m_Path, pTemFile); } //Helper.LogError("load file id:"+pTemFile.m_id+" start:"+pTemFile.m_StartPos+" size:"+pTemFile.m_Size+" filename:"+pTemFile.m_Path); } fs.Close(); fs.Dispose(); return(true); }
/// <summary> /// 上传文件并返回缩略图大小 /// </summary> /// <param name="postedFile">上传的文件</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <param name="userId">用户ID</param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(HttpPostedFileBase postedFile, string key, out FileInfor fileInfor, out ThumbnailInfo thumbnailInfo, int userId) { string useridstr = userId.ToString(); if (useridstr.Length < 9) { for (int i = 0; i < (9 - useridstr.Length); i++) { useridstr = "0" + useridstr; } } string[] folders = new string[2]; folders[0] = useridstr.Substring(0, 3); folders[1] = useridstr.Substring(3, 3); ImageServiceClientProxy client = new ImageServiceClientProxy(); string fileKey; string fileExt; fileInfor = new FileInfor(); thumbnailInfo = new ThumbnailInfo(); FileMessage f = client.GetFileNameByUser(key, postedFile.ContentLength, postedFile.FileName, null, out fileKey, out fileExt, folders); if (f == FileMessage.Success) { try { fileInfor.FileName = fileKey; fileInfor.FileSize = postedFile.ContentLength; FileUploadMessage inValue = new FileUploadMessage(); inValue.FileName = fileKey; inValue.KeyName = key; inValue.FileExt = fileExt; inValue.SaveOrigin = true; if (postedFile.InputStream.CanRead) { //byte[] content = new byte[postedFile.ContentLength + 1]; //postedFile.InputStream.Read(content, 0, postedFile.ContentLength); //inValue.FileData = new MemoryStream(content); inValue.FileData = postedFile.InputStream; thumbnailInfo = client.UploadFileAndReturnInfo(inValue); inValue.FileData.Close(); inValue.FileData.Dispose(); } else { return(FileMessage.UnknowError); } return(FileMessage.Success); } catch { return(FileMessage.UnknowError); } } else { return(f); } }
/// <summary> /// 上传文件并返回缩略图大小a /// </summary> /// <param name="postedFile">上传的文件对象</param> /// <param name="key">服务端配置索引名</param> /// <param name="fileInfor">文件上传后返回的信息对象</param> /// <param name="thumbnailInfo">缩略图信息</param> /// <returns></returns> public static FileMessage UploadFileAndReturnInfo(HttpPostedFileBase postedFile, string key, out FileInfor fileInfor, string[] userFolder, out ThumbnailInfo thumbnailInfo) { var client = _imageService; string fileKey; string fileExt; fileInfor = new FileInfor(); thumbnailInfo = new ThumbnailInfo(); FileMessage f = client.GetFileNameByUser(key, postedFile.ContentLength, postedFile.FileName, postedFile.ContentType, out fileKey, out fileExt, userFolder); if (f == FileMessage.Success) { fileInfor.FileName = fileKey; fileInfor.FileSize = postedFile.ContentLength; fileInfor.FileExtName = fileExt; fileInfor.ResourceType = ContentType.GetResourceType(fileExt); try { FileUploadMessage inValue = new FileUploadMessage(); inValue.FileName = fileKey; inValue.KeyName = key; inValue.FileExt = fileExt; inValue.SaveOrigin = true; if (postedFile.InputStream.CanRead) { //byte[] content = new byte[postedFile.ContentLength + 1]; //postedFile.InputStream.Read(content, 0, postedFile.ContentLength); //inValue.FileData = new MemoryStream(content); inValue.FileData = postedFile.InputStream; switch (fileInfor.ResourceType) { case ResourceType.Sound: long length; var t = SaveSound(inValue, out length); fileInfor.Length = length; fileInfor.Width = (int)length; LoggerManager.Current().Warn(fileInfor.Width); return(t); } int width, height; GetSize(postedFile, out width, out height); fileInfor.Width = width; fileInfor.Height = height; thumbnailInfo = client.UploadFileAndReturnInfo(inValue); inValue.FileData.Close(); inValue.FileData.Dispose(); } else { return(FileMessage.UnknowError); } return(FileMessage.Success); } catch { return(FileMessage.UnknowError); } } else { return(f); } }