[英]Image saving into database from a folder and get back into the UI
我的應用程序(c#)中有一個字段,用於將圖像保存到數據庫中。 我編寫了以下代碼,將圖像保存到文件夾中,然后將路徑保存到數據庫中。 但是圖像沒有保存到文件夾中。
string imgName = FileUpload1.FileName.ToString();
string imgPath = null;
if (imgName == "")
{
//int taxiid = Convert.ToInt32(HiddenField1.Value);
Taxi t = null;
t = Taxi.Owner_GetByID(tx.Taxi_Id, USM.OrgId);
imgPath = t.CarImage;
}
else
{
imgPath = "ImageStorage/" + imgName;
}
FileUpload1.SaveAs(Server.MapPath(imgPath));
tx.CarImage = imgPath;
我認為您的問題是您將名稱添加到我嘗試的路徑中,對我來說,如果我這樣保存它,則可以正常工作:
FileUpload1.SaveAs(Server.MapPath(“ ImageStorage”)+ imgName);
就像@Rahul提到的那樣,添加一個try catch來防止錯誤。
然后你檢查
如果(imgName ==“”)
根據我的理解,imgName不是“”是不可能的,但是無論如何,您最好添加一個檢查文件上傳是否包含文件。
如果(FileUploadControl.HasFile)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.