Example #1
0
        /// <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;
            }
        }
Example #2
0
        public ThumbnailInfo UploadFileAndReturnInfo(FileUploadMessage request)
        {
            Log.Debug(string.Format("Upload file:\n filename:{0}\n fileext:{1}\n keyname:{2}", request.FileName, request.FileExt, request.KeyName));


            if (request.FileData.CanRead == false)
            {
                Log.Debug(string.Format("\"{0}\"文件不可读", request.FileName));

                request.FileData.Close();
                request.FileData.Dispose();

                return null;
            }


            _imageSetting = (ImageSettingConfig)ConfigurationManager.GetSection("imageSetting");


            _imageElement = _imageSetting.ImageCollection[request.KeyName];


            _fileFolder = String.IsNullOrEmpty(_imageElement.RootFolder) ? _imageSetting.Folder : _imageElement.RootFolder;

            string filePath = Path.Combine(_fileFolder, request.FileName + (_imageElement.AsNormalFile ? string.Empty : FileTempExt) + "." + request.FileExt);

            var fileInfo = new FileInfo(filePath);


            if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
            {
                Directory.CreateDirectory(fileInfo.Directory.FullName);
            }

            try
            {
                using (var sourceStream = request.FileData)
                {
                    using (var targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {

                        const int bufferLen = 4096;
                        byte[] buffer = new byte[bufferLen];
                        int count = 0;
                        while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                        {
                            targetStream.Write(buffer, 0, count);
                        }

                        targetStream.Close();
                        sourceStream.Close();
                        request.FileData.Close();
                        request.FileData.Dispose();
                    }

                }

                if (Pexts.Contains(request.FileExt, StringComparer.OrdinalIgnoreCase) && !_imageElement.AsNormalFile)
                {
                    ThumbnailInfo thumbnailInfoes = new ThumbnailInfo();
                    thumbnailInfoes.Info = new Dictionary<string, long>();
                    thumbnailInfoes.Sizes = new Dictionary<string, ImageSize>();

                    foreach (ThumbElement thumb in _imageElement.ThumbCollection)
                    {
                        string thumbPath = filePath.Substring(0, filePath.IndexOf(FileTempExt)) + "_" + thumb.Width.ToString() + "x" + thumb.Height.ToString() + ".jpg";
                        try
                        {
                            int realWidth;
                            int realHeight;

                            Log.Debug("thumbPath:" + thumbPath);
                            long size = Thumbnail.Instance.MakeThumbnailPicAndReturnSize(filePath, thumbPath, thumb.Width, thumb.Height, thumb.Mode, _imageSetting.ImageQuality, out realWidth, out realHeight, thumbnailInfoes.ExifInfos);

                            thumbnailInfoes.Info.Add(thumb.Key, size);
                            thumbnailInfoes.Sizes.Add(thumb.Key, new ImageSize(realWidth, realHeight));
                        }
                        catch (Exception ex)
                        {
                            Log.Warn(ex);
                            continue;
                        }
                    }

                    return thumbnailInfoes;
                }


                else if (Pexts.Contains(request.FileExt, StringComparer.OrdinalIgnoreCase) && request.ConvertToJPG && _imageElement.AsNormalFile)
                {
                    using (Image bitmap = Image.FromFile(filePath))
                    {

                        bitmap.Save(filePath.Substring(0, filePath.IndexOf("." + request.FileExt)) + ".jpg", ImageFormat.Jpeg);
                    }

                }


                if (!request.SaveOrigin)
                {
                    File.Delete(filePath);
                }
            }
            catch (Exception ex)
            {
                Log.Error(string.Format("上传文件\"{0}\"发生错误:{1}", request.FileName, ex.Message));
            }



            Log.Debug(string.Format("Upload file successful.file: {0}, filekey: {1}", request.FileName, request.KeyName));

            return new ThumbnailInfo();
        }
Example #3
0
        /// <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);
            }
        }
Example #4
0
        /// <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);
            }
        }
Example #5
0
        /// <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;
            }
        }
Example #6
0
        /// <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;
            }
        }
Example #7
0
        /// <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);
        }