在ASP.NET MVC 5中上载文件 [英] Upload files in ASP.NET MVC 5

查看:84
本文介绍了在ASP.NET MVC 5中上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,我正在尝试上传asp.net mvc 5中的图像并将其保存到数据库中,直到现在我已将文件复制到一个文件夹中,并且我正在尝试将该路径保存到我的数据库中,这是我的控制器代码...



Hello everyone, I am trying to upload an image in asp.net mvc 5 and save it to a database, until now I have copied my file in a folder, and I'm trying to save that route to my database, this is my controller code...

[HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            var model = new ImageModel();
            try
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content/Uploads"), fileName);
                    model.ImageServerPath = path;
                    file.SaveAs(path);
                    db.images.Add(model); //Database
                    db.SaveChanges();
                }
                ViewBag.Message = "Upload Successful";
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ViewBag.Message = "Upload Failed";
                return RedirectToAction("Upload");
            }
        }



在我看来,我唯一拥有的是文件输入和按钮,这是我的模型代码。 ..


In my view the only thing that I have is a file input, and a button, and this is my model code...

public class ImageModel
   {
       [Key]
       public int Id { get; set; }
       public string ImageServerPath { get; set; }
   }



我知道,问题是,最好的办法是什么?



此外,我需要在我的索引中显示所有图像,但这也不起作用,不显示任何内容,只有引导程序中的缩略图与其中的alt文本,我怎么能得到我上传的文件的路线,并在我的索引文件中显示该路线中的图像内容?这是我的代码...


And know, my question is, How is the best way to do this?.

Besides, I need to show all the images in my index, but that doesn't work either, don't show nothing, only thumbnails from bootstrap with the alt text inside of it, how can I get the route of my uploaded files, and show the content of the image in that route in my index file?, here is my code...

@model IEnumerable<ArtStore.Models.ImageModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@foreach (var item in Model)
{
    string path = item.ImageServerPath;
    <img src="@item.ImageServerPath" alt="Blah" />
}



我在这里做什么?


what i'm not doing here?

推荐答案

使用
<img src="" /> 





首先检查图像是否存在于服务器文件夹中,如果不存在则将其从数据库下载到服务器文件夹。



然后你可以直接使用图像路径,例如





First check the image is present in server folder or not, if not present download it from database to server folder.

Then you can directly use the image path like

<img src="/ImageFolderName/Subfoler/image.jpg">
</img>





其实问题是





Actually problem is

@item.ImageServerPath





此变量包含服务器的图像文件夹路径。浏览器不需要此完整路径。使用firebug检查图像src ..您将获得有关错误的更多详细信息..



This variable contain image folder path of server..This full path is not required in browser. Use firebug check the image src..You will get more details about your error..


这篇关于在ASP.NET MVC 5中上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆