Exemplo n.º 1
0
        public object Upload(byte[] uploadFileBytes, string uploadFileName)
        {
            object model;

            try
            {
                string fileMd5  = UpYun.BytesMd5(uploadFileBytes);
                string bucket   = "panpan";     // 空间名称
                string username = "******";     // 操作员
                string password = "******";   // 密码


                UpYun upyun = new UpYun(bucket, username, password);
                // 设置待上传文件的 Content-MD5 值(如又拍云服务端收到的文件MD5值与用户设置的不一致,将回报 406 Not Acceptable 错误)
                //upyun.setContentMD5(fileMd5);
                upyun.ContentMd5 = fileMd5;

                // 上传文件时可使用 upyun.writeFile("/a/test.jpg",postArray, true);
                // 进行父级目录的自动创建(最深10级目录)
                string upYunFilePath = string.Format("{0}/{1}{2}", this.UpYunPath, fileMd5, Path.GetExtension(uploadFileName));
                bool   success       = upyun.WriteFile(upYunFilePath, uploadFileBytes, true);
                if (success)
                {
                    // 返回图片在UpYun中的http访问地址
                    Result.Url   = string.Format("{0}{1}", "http://static.qpanpan.cn", upYunFilePath);
                    Result.State = UploadState.Success;
                }
                else
                {
                    Result.State        = UploadState.FileAccessError;
                    Result.ErrorMessage = "上传图片到又拍云失败";
                    Logger.Error(Result.ErrorMessage);
                }
            }
            catch (Exception err)
            {
                Result.State        = UploadState.FileAccessError;
                Result.ErrorMessage = err.Message;
                Logger.Error(Result.ErrorMessage);
            }
            finally
            {
                model = new
                {
                    state    = GetStateMessage(Result.State),
                    url      = Result.Url,
                    title    = Result.OriginFileName,
                    original = Result.OriginFileName,
                    error    = Result.ErrorMessage
                };
            }
            return(model);
        }
Exemplo n.º 2
0
        public object UpLoad(string imageName, Stream imageStream)
        {
            object model;

            try
            {
                byte[] imageBytes = UpYun.StreamToBytes(imageStream);

                string upYunFilePath = string.Format("/images/qrcode/{0}", imageName);
                UpYun  upYun         = new UpYun("panpan", "panpan", "panpan88")
                {
                    ContentMd5 = UpYun.BytesMd5(imageBytes),
                };
                bool success = upYun.WriteFile(upYunFilePath, imageBytes, true);
                if (success)
                {
                    // 返回图片在UpYun中的http访问地址
                    Result.Url   = "http://static.qpanpan.cn" + upYunFilePath;
                    Result.State = UploadState.Success;
                }
                else
                {
                    Result.State        = UploadState.FileAccessError;
                    Result.ErrorMessage = "上传图片到又拍云失败";
                    Logger.Error(Result.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                Result.State        = UploadState.FileAccessError;
                Result.ErrorMessage = ex.Message;
                Logger.Error(Result.ErrorMessage);
            }
            finally
            {
                model = new
                {
                    state    = GetStateMessage(Result.State),
                    url      = Result.Url,
                    title    = Result.OriginFileName,
                    original = Result.OriginFileName,
                    error    = Result.ErrorMessage
                };
            }
            return(model);
        }
Exemplo n.º 3
0
        public async Task <bool> SaveUploadFileOnUPYunAsync(UpYun upyun, IFormFile formFile, string uploadDirPath, string fileName)
        {
            if (formFile.Length > 0)
            {
                // string fileExt = Path.GetExtension(formFile.FileName); //文件扩展名,不含“.”
                // long? fileSize = formFile.Length; //获得文件大小,以字节为单位

                if (!Directory.Exists(uploadDirPath))
                {
                    Directory.CreateDirectory(uploadDirPath);
                }
                var          filePath  = uploadDirPath + fileName;
                MemoryStream stmMemory = new MemoryStream();
                await formFile.CopyToAsync(stmMemory).ConfigureAwait(false);

                byte[] imageBytes = stmMemory.ToArray();
                return(upyun.WriteFile($"{uploadDirPath}{fileName}", imageBytes, true));
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public JsonResult SaveImage(string image)
        {
            try
            {
                // 获取base64字符串
                byte[]       imgBytes = Convert.FromBase64String(image);
                string       md5      = UpYun.BytesMd5(imgBytes);
                UploadResult ur       = new UploadResult()
                {
                    State          = UploadState.Unknown,
                    OriginFileName = string.Format("{0}.jpg", _random.Next(100000, 10000000)),
                };

                UpYun upYun = new UpYun("panpan", "panpan", "panpan88")
                {
                    ContentMd5 = md5,
                };

                string file = string.Format("/12310720112/{0}.jpg", md5);
                upYun.WriteFile(file, imgBytes, true);

                return(Json(new
                {
                    status = "ok",
                    msg = "http://static.qpanpan.cn" + file,
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    status = "error",
                    msg = ex.Message,
                }));
            }
        }