我一直在尝试通过文件上传控件上传图片。该文件未上传,我不断收到以下错误:"Cannot access a closed file."
我不确定为什么会收到此错误,我们将不胜感激。
我的代码:首先是抓取图片并创建'预览的代码
protected void btnupload_Click(Object sender, EventArgs e)
{
Session["Image"] = flupGalImg.PostedFile;
Stream fs = flupGalImg.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
imgGalImg.ImageUrl = "data:image/png;base64," + base64String;
imgGalImg.Visible = true;
}
我的代码调用我的业务逻辑代码:
else if(btnaddedit.Text == "Add item")
{
if(txtItemTitle.Text != "")
{
if(txtItemDescription.Text != "")
{
if(Session["Image"] != null)
{
HttpPostedFile postedFile = (HttpPostedFile)Session["Image"];
int alb;
int.TryParse(hdnAlbId.Value, out alb);
if(newsLogic.CreateNewGalleryItem(txtItemTitle.Text, txtItemDescription.Text, alb, postedFile) == true)
{
// Do something.
}
}
}
}
}
最后是我的业务逻辑代码:
// create gallery item
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
public bool CreateNewGalleryItem(string itemTitle, string itemDescription, int itemAlbum, HttpPostedFile itemFile)
{
string folderpath = @"~/Images/GalleryImages/";
string filename = Path.GetFileName(itemFile.FileName.ToString());
bool itemCreated = false;
string itemImgPath;
try
{
itemImgPath = Path.Combine(folderpath + filename);
string itemImgUploadPath = Path.Combine(HttpContext.Current.Server.MapPath(folderpath) + filename);
itemFile.SaveAs(itemImgUploadPath);
galAdp.CreateGalleryItem(itemTitle, itemDescription, itemImgPath, itemAlbum);
itemCreated = true;
}
catch (Exception er)
{
itemCreated = false;
string consmsg = er.Message;
}
return itemCreated;
}
我的图像预览'工作得很好。在业务逻辑的这一行上抛出了错误:itemFile.SaveAs(itemImgUploadPath);
当我进行谷歌搜索(很多谷歌搜索)时,我读到它可能是文件大小,所以我增加了web.config中允许的最大内容,但没有任何改变。
关于我做错了什么的想法?