Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 将路径(图像)转换为HttpPostedFileBase_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 将路径(图像)转换为HttpPostedFileBase

Asp.net mvc 将路径(图像)转换为HttpPostedFileBase,asp.net-mvc,Asp.net Mvc,我为用户提供上传图像的功能 但如果没有完成,我想在images文件夹中设置一个默认图像 如何将类型为“HttpPostedFileBase”的model属性设置为位于以编程方式设置的路径上的映像 模型属性: public HttpPostedFileBase UploadedImage { get; set; } 我尝试过这些,但没有成功 我得到的是:无法将类型“string”转换为“System.Web.HttpPostedFileBase” if (userProfileForSave

我为用户提供上传图像的功能

但如果没有完成,我想在images文件夹中设置一个默认图像

如何将类型为“HttpPostedFileBase”的model属性设置为位于以编程方式设置的路径上的映像

模型属性:

 public HttpPostedFileBase UploadedImage { get; set; }
我尝试过这些,但没有成功

我得到的是:无法将类型“string”转换为“System.Web.HttpPostedFileBase”

 if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage == null)
 {
   // Set a default.
   userProfileForSaveVM.UserProfileSingleVM.UploadedImage = 
   "F:\\GbngWebClient\\GbngWebClient\\Images\\Avatar.png";
 }
 if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage == null)
 {
   // Set a default.
   byte[] imageArray = 
   System.IO.File.ReadAllBytes(@F:\\GbngWebClient\\GbngWebClient\\Images\\Avatar.png");
   userProfileForSaveVM.UserProfileSingleVM.UploadedImage = imageArray;
 }
我得到的这个:无法将属性或索引器“HttpPostedFileBaseInputStream”分配给它--它是只读的

 if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage == null)
 {
   // Set a default.
   String pathImage = Server.MapPath("F:\\GbngWebClient\\GbngWebClient\\Images\\Avatar.png");
   userProfileForSaveVM.UserProfileSingleVM.UploadedImage.InputStream = new FileStream(pathImage, FileMode.Open);
 }
我得到的是:无法将类型“byte[]”转换为“System.Web.HttpPostedFileBase”

 if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage == null)
 {
   // Set a default.
   userProfileForSaveVM.UserProfileSingleVM.UploadedImage = 
   "F:\\GbngWebClient\\GbngWebClient\\Images\\Avatar.png";
 }
 if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage == null)
 {
   // Set a default.
   byte[] imageArray = 
   System.IO.File.ReadAllBytes(@F:\\GbngWebClient\\GbngWebClient\\Images\\Avatar.png");
   userProfileForSaveVM.UserProfileSingleVM.UploadedImage = imageArray;
 }

我不知道为什么要创建这种类型的类。该类用于从HTTP请求接收文件数据。通常,您使用该类的输入流从该类获取数据。msdn文档在这里有一个示例:


您要做的是从HttpPostedFileClass获取文件流,如果没有,则从默认位置获取该文件流。您可以声明一个空内存流作为开始,并在适当的情况下从HttpPostedFile或您的本地资源中填充该流。

尝试在视图中使用check null,如:

if (userProfileForSaveVM.UserProfileSingleVM.UploadedImage != null)
{
   <img src= "@Url.Content("~/uploads/FileUpload12011_03_02_11_49_22.jpg")" alt="IMAGES" />
}
if(userProfileForSaveVM.UserProfileSingleVM.UploadedImage!=null)
{
}