我需要将图像路径上传到数据库 [英] I need to Upload image path to database
本文介绍了我需要将图像路径上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下查询上传照片
I used following query to upload a photo
objPhoto.MembershipID = Convert.ToInt32(Session["MembershipID"]);
DataTable dt = objPhoto.ViewRecord();
string file = imgUpload.FileName;
string fname = Session["MembershipID"].ToString();
string st = "~/UploadImages/";
string str = "-";
if (dt.Rows.Count > 0)
{
imgUpload.PostedFile.SaveAs(Server.MapPath("~/UploadImages/") + fname + str + dt.Rows.Count + Path.GetExtension(file));
objPhoto.UploadImages = st + fname + str + dt.Rows.Count + Path.GetExtension(file);
objPhoto.ImageID = objPhoto.ViewData();
imgPreview.ImageUrl = "~/UploadImages/" + fname + str + dt.Rows.Count + Path.GetExtension(file) + "";
}
上面提到的编码工作正常。但是我在另一种形式中使用相同的编码它显示以下错误
fileStream不会打开Win32设备,如磁盘分区和磁带驱动器。避免在路径中使用\\.\
。
推荐答案
检查文件是否存在名字有保留字。您将无法创建具有保留名称的文件。
使用System.IO.Path.GetInvalidPathChars()&System.IO.Path.GetInvalidFileNameChars();
这样您就可以获得无效字符的完整列表,这些字符不能是有效的文件名。在创建文件之前,你需要从文件名中替换那些字符。
Check if the file name has reserved words. You won't be able to create files with reserved names.
Use System.IO.Path.GetInvalidPathChars()& System.IO.Path.GetInvalidFileNameChars();
So that you can get full list of invalid charecters which cannot be a valid file name. You need to replace those chars from the filename before you create the file.
你好,你可以参考这是MVC但是你可以参考这个...它可以帮助你>
..
Hello,YOu Can Refer This Is Of MVC But You Can Refer this ...It Might Help You
..
public ActionResult Display(HttpPostedFileBase file)
{
if (file != null)
{
EmployeeEntities context = new EmployeeEntities();
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/" + ImageName);
// save image in folder
file.SaveAs(physicalPath);
//save new record in database
Image newRecord = new Image
{
fname = Request.Form["fname"],
lname = Request.Form["lname"],
ImageUrl = ImageName
};
_imgRepo.Insert(newRecord);
context.SaveChanges();
PhotoForSingleItem tempmodel = new PhotoForSingleItem();
tempmodel.Images = _imgRepo.GetEntities().ToList();
return View("Display",tempmodel);
}
return null;
..
此代码是控制器...它肯定会帮助你..
在_imgRepo.insert(newrecord)
context.savechanges(); ////你可以写您的插入代码..
但主要部分是物理路径..
谢谢
..
This Code Is OF Controller...It Will surely Help You..
In _imgRepo.insert(newrecord)
context.savechanges();////You Can Write Your Insert Code ..
But Main Part Is Of PhysicalPath..
Thanks
这篇关于我需要将图像路径上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文