Пример #1
0
    private UploadResult GetResult(byte[] uploadFileBytes)
    {
        var ret = QiniuHelper.GetResult(uploadFileBytes);

        if (ret.OK)
        {
            Result.Url   = QiniuHelper.GetUrl(ret.key);;
            Result.key   = ret.key;
            Result.State = UploadState.Success;
        }

        return(Result);
    }
Пример #2
0
        public string UploadFileAfterCheckRepeat(FileInfo file, string uid,
                                                 ref string file_url, ref string file_name, bool DeleteFileAfterUploadToQiniu = true)
        {
            try
            {
                if (!file.Exists)
                {
                    throw new Exception("无法在磁盘上找到文件");
                }

                var dal = new UpFileDal();

                var dbmodel = new UpFileModel();
                dbmodel.UserID     = uid;
                dbmodel.FileName   = file.Name;
                dbmodel.FileExt    = file.Extension;
                dbmodel.FileSize   = (int)file.Length;
                dbmodel.FilePath   = file.FullName;
                dbmodel.CreateTime = DateTime.Now;
                //获取文件md5值
                dbmodel.FileMD5 = SecureHelper.GetFileMD5(dbmodel.FilePath);
                if (!ValidateHelper.IsPlumpString(dbmodel.FileMD5))
                {
                    throw new Exception("获取文件MD5失败");
                }
                //判断文件是否存在于七牛
                var  qiniu_file        = QiniuHelper.FindEntry(dbmodel.FileMD5);
                bool FindInQiniu       = qiniu_file.HasFile();
                bool uploadToQiniuByMe = false;
                if (FindInQiniu)
                {
                    //直接拿七牛中的文件地址
                    dbmodel.FileUrl = QiniuHelper.GetUrl(dbmodel.FileMD5);
                }
                else
                {
                    var url = QiniuHelper.Upload(file.FullName, dbmodel.FileMD5);
                    dbmodel.FileUrl = url;
                    //标记文件是我上传到七牛的
                    uploadToQiniuByMe = true;
                }
                //运行到这里,七牛已经有文件了
                //判断是否要添加到数据库
                var res = AddFile(dbmodel);
                if (ValidateHelper.IsPlumpString(res))
                {
                    //如果是我上传到七牛的并且保存本地数据库失败就删除
                    if (uploadToQiniuByMe)
                    {
                        QiniuHelper.Delete(dbmodel.FileMD5);
                    }
                    return("保存到数据库失败");
                }

                file_name = dbmodel.FileName;
                file_url  = dbmodel.FileUrl;

                return(SUCCESS);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (DeleteFileAfterUploadToQiniu)
                {
                    file.Delete();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            var imageFile = GetImage();

            if (imageFile != null)
            {
                if (string.IsNullOrEmpty(StrGuid))
                {
                    var info = new PhotoInfo();

                    var ret = QiniuHelper.GetResult(imageFile);
                    if (ret.OK)
                    {
                        info.ImageUrl = QiniuHelper.GetUrl(ret.key);
                        info.ImageKey = ret.key;

                        info.Name    = tbName.Text;
                        info.Tag     = tbTag.Text;
                        info.IsCover = chkCover.Checked;

                        var user = HttpContext.Current.Session["UserInfo"] as UserInfo;
                        if (user != null)
                        {
                            info.UserGuid = user.Guid;
                        }

                        info.Createtime = DateTime.Now;
                        info.AlbumGuid  = AlbumGuid;

                        PhotoBll.Add(info);

                        ScriptHelper.AlertAndRedirect("添加成功", "PhotoList.aspx?albumguid=" + AlbumGuid);
                    }
                    else
                    {
                        Util.ScriptHelper.Alert2("上传失败");
                    }
                }
                else
                {
                    var info = PhotoBll.GetModel(StrGuid);

                    var ret = QiniuHelper.GetResult(imageFile);
                    if (ret.OK)
                    {
                        QiniuHelper.DeleteData(info.ImageKey);

                        info.ImageUrl = QiniuHelper.GetUrl(ret.key);
                        info.ImageKey = ret.key;

                        info.Name    = tbName.Text;
                        info.Tag     = tbTag.Text;
                        info.IsCover = chkCover.Checked;

                        var user = HttpContext.Current.Session["UserInfo"] as UserInfo;
                        if (user != null)
                        {
                            info.UserGuid = user.Guid;
                        }

                        info.Createtime = DateTime.Now;
                        info.AlbumGuid  = AlbumGuid;

                        PhotoBll.Update(info);

                        ScriptHelper.AlertAndRedirect("修改成功", "PhotoList.aspx?albumguid=" + AlbumGuid);
                    }
                    else
                    {
                        Util.ScriptHelper.Alert2("上传失败");
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(StrGuid))
                {
                    Util.ScriptHelper.Alert2("没有文件");
                }
                else
                {
                    var info = PhotoBll.GetModel(StrGuid);

                    info.Name    = tbName.Text;
                    info.Tag     = tbTag.Text;
                    info.IsCover = chkCover.Checked;

                    PhotoBll.Update(info);

                    ScriptHelper.AlertAndRedirect("修改信息成功", "PhotoList.aspx?albumguid=" + AlbumGuid);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            var imageFile = GetImage();

            if (string.IsNullOrEmpty(StrGuid))
            {
                var info = new CategoryInfo();

                info.Name       = tbName.Text;
                info.OrderId    = ConvertHelper.GetInt(tbOrderId.Text);
                info.ParentGuid = ddlParentCategory.SelectedValue;
                var user = HttpContext.Current.Session["UserInfo"] as UserInfo;
                if (user != null)
                {
                    info.UserGuid = user.Guid;
                }
                var blog = HttpContext.Current.Session["BlogInfo"] as BlogInfo;
                if (blog != null)
                {
                    info.BlogGuid = blog.Guid;
                }
                if (imageFile != null)
                {
                    var ret = QiniuHelper.GetResult(imageFile);
                    if (ret.OK)
                    {
                        info.ImageUrl = QiniuHelper.GetUrl(ret.key);
                        info.ImageKey = ret.key;
                    }
                    else
                    {
                        ScriptHelper.Alert("图片上传失败");
                    }
                }

                CategoryBll.Add(info);
            }
            else
            {
                var info = CategoryBll.GetModel(StrGuid);

                info.Name       = tbName.Text;
                info.OrderId    = ConvertHelper.GetInt(tbOrderId.Text);
                info.ParentGuid = ddlParentCategory.SelectedValue;
                if (imageFile != null)
                {
                    var ret = QiniuHelper.GetResult(imageFile);
                    if (ret.OK)
                    {
                        info.ImageUrl = QiniuHelper.GetUrl(ret.key);
                        info.ImageKey = ret.key;
                    }
                    else
                    {
                        ScriptHelper.Alert("图片上传失败");
                    }
                }

                CategoryBll.Update(info);
            }


            Response.Redirect("CategoryList.aspx");
        }
Пример #5
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;

        var savePath  = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
        var localPath = Server.MapPath(savePath);

        try
        {
            #region  再需要储存文件到服务器

            //if (!Directory.Exists(Path.GetDirectoryName(localPath)))
            //{
            //    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
            //}
            //File.WriteAllBytes(localPath, uploadFileBytes);
            //Result.Url = savePath;
            //Result.State = UploadState.Success;

            #endregion

            #region   文件到七牛

            var ret = QiniuHelper.GetResult(uploadFileBytes);

            if (ret.OK)
            {
                Result.Url   = QiniuHelper.GetUrl(ret.key);
                Result.State = UploadState.Success;
            }

            #endregion
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }