如何上传图片和显示。 [英] How to upload image and display.

查看:80
本文介绍了如何上传图片和显示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在提供的框中立即上传图片和显示。我正在保存数据库的路径。所以再次在另一个页面上我要显示该图像。



代码视图



 <   div     style   =  float:left; margin-left:100px  >  
上传图片
< 输入 type = file name = image_icon id = img / >

< / div >





控制器代码



 < span class =code-keyword> public  ActionResult Create(FormCollection集合,sampleViewModel sampleVM,HttpPostedFileBase图像)
{
尝试
{
// if(image!= null)
if (sampleVM.image_icon!= null
{
// var imageName = image.FileName;
var imageName = newsVM.image_icon;
var filePathOriginal = Server.MapPath( /内容/图像);
// var filePathThumbnail = Server.MapPath(/ Content / Uploads / Thumbnails);
string savedFileName = Path.Combine(filePathOriginal,imageName);
image.SaveAs(savedFileName);
}
}
}





但是我的图像为空。并且当我从filedialogue浏览图像时也想显示图像。

解决方案

 //请在此处查看参考资料

http://www.aspdotnet-suresh.com/2014/12/how-to-upload-files-in-asp-net-mvc-razor.html [ ^ ]



http://www.c-sharpcorner.com/UploadFile/b696c4/how-to-upload-and-display-image-in-mvc/ [ ^ ]


I want to upload image and display immediately in box provided. I am saving path to database. So again on another page i want to display that image.

code in view

 <div style="float:left; margin-left:100px">
Upload Image
 <input type="file" name="image_icon" id="img" />

 </div>



code in controller

public ActionResult Create(FormCollection collection, sampleViewModel sampleVM, HttpPostedFileBase image)
{
 try
 {
 //if (image != null)
 if(sampleVM.image_icon!=null)
 {
     // var imageName = image.FileName;
     var imageName = newsVM.image_icon;
     var filePathOriginal = Server.MapPath("/Content/Image");
     //var filePathThumbnail = Server.MapPath("/Content/Uploads/Thumbnails");
     string savedFileName = Path.Combine(filePathOriginal, imageName);
     image.SaveAs(savedFileName);
 }
 }
}



But i am getting image as null. And also want display image when i browse image from filedialogue.

解决方案

//Pls check here for reference 

http://www.aspdotnet-suresh.com/2014/12/how-to-upload-files-in-asp-net-mvc-razor.html[^]


http://www.c-sharpcorner.com/UploadFile/b696c4/how-to-upload-and-display-image-in-mvc/[^]


这篇关于如何上传图片和显示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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