Imagebutton在uploads文件夹中看不到图像,显示为空白 [英] Imagebutton doesn't see images in uploads folder, shows blank

查看:185
本文介绍了Imagebutton在uploads文件夹中看不到图像,显示为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET WebForms项目中,我有一个名为Uploads的文件夹,其中包含一些.jpg照片图像文件。如果我右键单击它并选择在文件资源管理器中打开文件夹,它会显示我列出的五个图像文件,每个图像文件都有一个.jpg扩展名。



我在表单上有一个ImageButton控件。

我选择了ImageButton控件并在属性窗口中。

我点击ImageUrl的省略号按钮,

我选择'上传'文件夹。

我是否选择图像文件(* gif,* jpg,* jpg,* bmp,* wmf,*。png)甚至全部文件(*。*),在任何一种情况下,它都显示一个空文件列表,好像文件夹中没有文件。



因此,当我将完整的文件路径分配给ImageButton1.ImageUrl,图像不会出现但仍为空白。但即使在设计时,ImageButton也根本没有看到这些图像文件。我该如何解决这个问题?



我确保在WWW服务\常见HTTP功能中选择了静态内容选项。 />


我尝试过:



 HttpPostedFile postedFile = FileUpload1.PostedFile; 
string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = postedFile.ContentLength;
string [] exts = {.jpg,。bmp,。gif,。png};

if(exts.Contains(fileExtension.ToLower()))
{
var dirUploads =〜/ Uploads /;
var virtualPath = dirUploads + fileName;
var filePath = System.Web.HttpContext.Current.Server.MapPath(virtualPath);
if(System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
postedFile.SaveAs(filePath.ToString());
ImageButton1.ImageUrl = filePath.ToString();
}

解决方案

图像网址将是服务器驱动器上图像的位置客户显然无法访问。图像URL必须是服务器上图像的http路径。尝试将ImageUrl设置为virtualPath,您可能需要类似



 ImageButton1.ImageUrl = VirtualPathUtility.ToAbsolute(virtualPath); 


In my ASP.NET WebForms project, I have a folder called 'Uploads' that contains a few .jpg photo image files. If I right-click it and select "Open Folder in File Explorer", it shows my five image files listed, and each one has a '.jpg' extension.

I have an ImageButton control on the form.
I select the ImageButton control and in the properties window.
I click the ellipsis button for ImageUrl,
I select the 'Uploads' folder.
Whether I select "Image Files(*gif,*jpg,*jpg,*bmp,*wmf,*.png)" or even "All Files(*.*)", in either case, it shows an empty file list, as if there are no files in the folder.

As a result, when I assign the full file path to the ImageButton1.ImageUrl, the image does not appear but remains blank. But again even at design time the ImageButton is not seeing these image files at all. How can I fix this?

I made sure that the Static Content option is selected in WWW Service \ Common HTTP Features.

What I have tried:

HttpPostedFile postedFile = FileUpload1.PostedFile;
string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetExtension(fileName);
int fileSize = postedFile.ContentLength;
string[] exts = { ".jpg", ".bmp", ".gif", ".png" };

if (exts.Contains(fileExtension.ToLower()))
{
    var dirUploads = "~/Uploads/";
    var virtualPath = dirUploads + fileName;
    var filePath = System.Web.HttpContext.Current.Server.MapPath(virtualPath);
    if (System.IO.File.Exists(filePath))
    {
        System.IO.File.Delete(filePath);
    }
    postedFile.SaveAs(filePath.ToString());
    ImageButton1.ImageUrl = filePath.ToString();
}

解决方案

The image url is going to be the location of the image on the server's drive which the client obviously can't access. The image url has to be an http path to the image on the server. Try setting ImageUrl to virtualPath instead, you might need something like

ImageButton1.ImageUrl = VirtualPathUtility.ToAbsolute(virtualPath);


这篇关于Imagebutton在uploads文件夹中看不到图像,显示为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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