简体   繁体   English

将绝对路径替换为asp.net中的相对路径

[英]Replace absolute to relative path in asp.net

I have a upload function in ASP.NET MVC 4.5.2 how upload picture to ~/Uploads/. 我在ASP.NET MVC 4.5.2中具有上传功能,如何将图片上传到〜/ Uploads /。

When I want to display image with following code: 当我想用以下代码显示图像时:

<img src="@item.ImagePath" alt="Image" />

I obtain: 我得到:

<img src="c:\users\yanni\documents\visual studio 2015\Projects\MyProject\MyProject\Uploads/references/postfinance.PNG" alt="Image" />

My question is to know how i can replace c:\\users ... by "~/Uploads/filename.ext". 我的问题是知道我如何用“〜/ Uploads / filename.ext”替换c:\\ users...。

Maybe it's important to specify: in my Database, i have the full link: c:\\users\\yanni\\documents\\visual studio 2015\\Projects\\MyProject\\MyProject\\Uploads/references/postfinance.PNG 也许重要的是要指定:在我的数据库中,我具有完整链接:c:\\ users \\ yanni \\ documents \\ visual studio 2015 \\ Projects \\ MyProject \\ MyProject \\ Uploads / references / postfinance.PNG

referenced by this ControllerCode: 此ControllerCode引用:

references.ImagePath = HttpContext.Request.PhysicalApplicationPath + "Uploads/references/" + ImagePath.FileName;
ImagePath.SaveAs(references.ImagePath);

Thanks per advance for your help 非常感谢您的帮助

Try out something like this: 试试这样的事情:

<img src="@Url.Content("~/Uploads/Image.png")" />

If you want image to be fetched according to the current request 如果要根据当前请求获取图像

<img src="@Url.Content("~/Uploads/" + FileName)" />

where file name is nothing but the name of the image or 其中文件名不过是图像名称或

<img src="@Url.Content("~/Uploads/" + System.IO.Path.GetFileName(@item.ImagePath))" />

I really don't get why you say you upload to "~/Uploads", but later you store ImagePath as "Uploads/ references /...". 我真的不明白为什么您说要上传到“〜/ Uploads”,但是后来您将ImagePath存储为“ Uploads / references / ...”。 Why do you append that dir? 为什么要附加该目录? Do you move that file there? 您将文件移到那里吗?

In general: I'd better store image path in the database as relative, so you can anytime relocate the project. 通常,我最好将图像路径作为相对路径存储在数据库中,因此您可以随时重定位项目。

var fileName = "~/Uploads/" + ImagePath.FileName;
references.ImagePath = fileName;

And if you ever need absolute path of the image on your server (which depends on where is your project located on server), you can get it in controller by 如果您需要服务器上映像的绝对路径(取决于项目在服务器上的位置),则可以通过以下方式在控制器中获取它:

var realPath = Server.MapPath(fileName);

Thanks to this approach will this code: 由于这种方法,该代码将:

 <img src="@item.ImagePath" alt="Image" />

render to: 呈现给:

 <img src="~/Uploads/postfinance.PNG" alt="Image" />

Which is probably what you want... 这可能是您想要的...

Solution founded: 解决方案已建立:

public ActionResult Create([Bind(Include = "ID,Compagny,Website,Logo")] References references, HttpPostedFileBase ImagePath)
        {
            if (ModelState.IsValid)
            {
                var fileName = Path.GetFileName(ImagePath.FileName);
                references.ImagePath = "/Uploads/References/" + fileName;
                ImagePath.SaveAs(Server.MapPath("~/Uploads/References/" + fileName));

                db.References.Add(references);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(references);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM