C# 将映像上载到服务器位置mvc5
我有一个应用程序,允许管理员上传图片的商店。目前,它使用服务器路径在本地工作:C# 将映像上载到服务器位置mvc5,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个应用程序,允许管理员上传图片的商店。目前,它使用服务器路径在本地工作: string ImageName = System.IO.Path.GetFileName(file.FileName); string physicalPath = Server.MapPath("~/Images/"+ImageName); //which is local project folder. 但是,当我发布它时,它不再显示上载的图像或上载的内容。如何将服务器路径更改为存储在服务器上,例如,如果网站
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/"+ImageName); //which is local project folder.
但是,当我发布它时,它不再显示上载的图像或上载的内容。如何将服务器路径更改为存储在服务器上,例如,如果网站地址是www.google.com,是否有办法从服务器而不仅仅是本地实现这一点
以下是完整的图像上载代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerID,DiscountLevelID,LoyaltyLevelID,CustomerCompanyName,CustomerName,CustomerSurname,CustomerGUID,CustomerStatus,CustomerAddress,CustomerTel,CustomerCel,CustomerNumber,CustomerContact,CustomerLogo,CustomerLogoPath,LastStoreCustomerSyncID")] Customer customer, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
string ImageName = System.IO.Path.GetFileName(file.FileName);
string physicalPath = Server.MapPath("~/Images/");
if (!Directory.Exists(physicalPath))
Directory.CreateDirectory(physicalPath);
string physicalFullPath = Path.Combine(physicalPath, ImageName);
file.SaveAs(physicalFullPath);
customer.CustomerLogo = ImageName;
customer.CustomerLogoPath = physicalFullPath;
db.Customers.Add(customer);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(customer);
}
使用此选项在索引中显示图像:
<td><img src=@Url.Content(@item.CustomerLogo) width="100" height="100" /></td>
要查看图像,您可以这样尝试:
<img src= "@Url.Content(@Model.CustomerLogo)" alt="Image" width="100" height="100" />
查看:
public class FileModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
@model MvcApplication.Models.FileModel
@using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file" })
@Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload</button>
}
public ActionResult Upload(FileModel model)
{
string fileName = Path.GetFileName(model.File.FileName);
string path = Server.MapPath("~/Files/");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string fullPath = Path.Combine(path, fileName);
model.File.SaveAs(fullPath);
return View();
}
要查看图像,可以尝试以下操作:
<img src= "@Url.Content(@Model.CustomerLogo)" alt="Image" width="100" height="100" />
查看:
public class FileModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
@model MvcApplication.Models.FileModel
@using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(x => x.File)
@Html.TextBoxFor(x => x.File, new { type = "file" })
@Html.ValidationMessageFor(x => x.File)
</div>
<button type="submit">Upload</button>
}
public ActionResult Upload(FileModel model)
{
string fileName = Path.GetFileName(model.File.FileName);
string path = Server.MapPath("~/Files/");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string fullPath = Path.Combine(path, fileName);
model.File.SaveAs(fullPath);
return View();
}
非常感谢。不过,我主要是想找到一个解决方案,将图像上传到服务器上,而不是存储在本地文件夹中。因为当我发布项目时,图像的上传不再有效字符串physicalPath=Server.MapPath(“~/Images/”+ImageName);如何更改路径,使其不存储在本地,而是存储在服务器上谢谢。使用的图像显示不起作用,使用模型时出现编译错误,因此我使用@item.CustomerLogo,然后使用“@Url.Content(model.CustomerLogo)”给出运行时错误try,我使用您建议的物理路径上载代码发布了它,但应用程序中断:(谢谢。不过,我主要是想寻求一种解决方案,将图像上载到服务器上,而不是存储在本地文件夹中。因为当我发布项目时,图像的上载不再有效String physicalPath=server.MapPath(“~/images/”+ImageName));如何更改路径,使其不存储在本地,而是存储在服务器上谢谢。使用model显示图像不起作用,使用model时出现编译错误,因此我使用@item.CustomerLogo,然后使用“@Url.Content(model.CustomerLogo)”给出运行时错误try我使用您建议的物理路径上传代码发布了它,但应用程序坏了:(