在 asp.net mvc 中上传文件时如何更改服务器映射路径和文件名?
How change server map path and file name when file uploaded in asp.net mvc?
我正在尝试使用 asp.net mvc 5
中的 entity framework 将图片保存在文件夹中并将路径存储在数据库中。
我做到了,但我有一些问题。
数据库中的图像路径保存如下:
C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods.jpg
如何将其更改为:~/Images/SubGoods/2.jpg
??
我想将图像名称更改为 primary key id
,我使用 pic =Convert.ToString( subgood.SubGoodID);
来完成它,但它节省了零:
C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods[=12=]
它总是保存 0
。我知道那是因为该行中的主键尚未生成。我的代码 primary Key id
在哪里生成的?
public ActionResult AddSubGood(SubGood subgood, HttpPostedFileBase UploadImage)
{
var MainGoodId = subgood.FKMainGoodID;
SubGoodRepositories blSubGood = new SubGoodRepositories();
string path="";
if (UploadImage != null)
{
string pic = System.IO.Path.GetFileName(UploadImage.FileName);
pic =Convert.ToString( subgood.SubGoodID);
path = System.IO.Path.Combine(
Server.MapPath("~/Images/SubGoods"), pic);
}
if (ModelState.IsValid)
{
subgood.FKMainGoodID = MainGoodId;
UploadImage.SaveAs(path);
subgood.SubGoodImage = path;
if (blSubGood.Add(subgood))
{
return JavaScript("alert('saved');");
}
else
{
return JavaScript("alert('didn't saved');");
}
}
else
{
return JavaScript("alert('error');");
}
}
Server.MapPath 将 return 你的虚拟路径(你不需要),你可以创建另一个变量并像这样连接:
string DbPath = "~/Images/SubGoods/"; // better to store in web.config file
DbPath = DbPath + ""//here you can query in table and find the last inserted primary key and increment it with '1'
您应该只保存文件名:
var fileName = Path.GetFileName(UploadImage.FileName);
然后当你想为用户获取文件时,你可以简单地用特定路径寻址文件名:
<img src="~/Content/Uploaded/@item.fileName" .../>
您还可以使用 Guid
:
生成随机文件名
var rondom = Guid.NewGuid() + fileName;
我正在尝试使用 asp.net mvc 5
中的 entity framework 将图片保存在文件夹中并将路径存储在数据库中。
我做到了,但我有一些问题。
数据库中的图像路径保存如下:
C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods.jpg
如何将其更改为:~/Images/SubGoods/2.jpg
??
我想将图像名称更改为 primary key id
,我使用 pic =Convert.ToString( subgood.SubGoodID);
来完成它,但它节省了零:
C:\Users\Shima\Documents\Visual Studio 2013\Projects\NP1\NP1\Images\SubGoods[=12=]
它总是保存 0
。我知道那是因为该行中的主键尚未生成。我的代码 primary Key id
在哪里生成的?
public ActionResult AddSubGood(SubGood subgood, HttpPostedFileBase UploadImage)
{
var MainGoodId = subgood.FKMainGoodID;
SubGoodRepositories blSubGood = new SubGoodRepositories();
string path="";
if (UploadImage != null)
{
string pic = System.IO.Path.GetFileName(UploadImage.FileName);
pic =Convert.ToString( subgood.SubGoodID);
path = System.IO.Path.Combine(
Server.MapPath("~/Images/SubGoods"), pic);
}
if (ModelState.IsValid)
{
subgood.FKMainGoodID = MainGoodId;
UploadImage.SaveAs(path);
subgood.SubGoodImage = path;
if (blSubGood.Add(subgood))
{
return JavaScript("alert('saved');");
}
else
{
return JavaScript("alert('didn't saved');");
}
}
else
{
return JavaScript("alert('error');");
}
}
Server.MapPath 将 return 你的虚拟路径(你不需要),你可以创建另一个变量并像这样连接:
string DbPath = "~/Images/SubGoods/"; // better to store in web.config file
DbPath = DbPath + ""//here you can query in table and find the last inserted primary key and increment it with '1'
您应该只保存文件名:
var fileName = Path.GetFileName(UploadImage.FileName);
然后当你想为用户获取文件时,你可以简单地用特定路径寻址文件名:
<img src="~/Content/Uploaded/@item.fileName" .../>
您还可以使用 Guid
:
var rondom = Guid.NewGuid() + fileName;