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); }
/// <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); } } }
/// <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"); }
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(); } }