图像控制不显示图像 [英] image control not showing image

查看:125
本文介绍了图像控制不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有两个图像控件,当我从图像文件夹中获取其图像网址然后显示图像时,当我从其他文件夹(Ads_Images)中获取图像网址时,它不显示图像,不知道什么是错的用它...

I have two image control on my page, when i take its image url from images folder then its showing image and when i take image url from other folder(Ads_Images) then it is not showing image, Don't know whats wrong with it...

<asp:Image ID="Image1" runat="server" ImageUrl="Ads_Images/7f0e7ef3-4cb8-40e0-a835-00314768a70a70-515.jpg"/>

<asp:Image ID="Image2" runat="server" ImageUrl="Images/imagenotavailable.jpg" />



其中Ads_Images和Images是我项目中的两个文件夹,Images是我的默认文件夹,在Ads_Images中,用户可以存储图像并保存到数据库的路径,我使用GUID添加我的图像。 />


where Ads_Images and Images are two folders in my project, Images is my default folder and in Ads_Images user can store images and save path to database, I am using GUID to add with my images.

protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;
        string filePath = string.Empty;
        string getPath = string.Empty;
        string pathToStore = string.Empty;
        if (FileUpload1.PostedFile != null)
        {
            fileName = FileUpload1.FileName;
            filePath = Server.MapPath("Ads_Images/" + System.Guid.NewGuid() + fileName);
            FileUpload1.SaveAs(filePath);
            SqlCommand cmd = new SqlCommand("insert into userimage (FileName, FilePath)" + " values(@FileName, @FilePath)", con);
            cmd.Parameters.AddWithValue("@FileName", fileName);
            int getPos = filePath.LastIndexOf("\\");
            int len = filePath.Length;
            getPath = filePath.Substring(getPos, len - getPos);
            pathToStore = getPath.Remove(0, 1);
            cmd.Parameters.AddWithValue("@FilePath", pathToStore);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            
        }
        }

protected void btnShow_Click(object sender, EventArgs e)
    {
        SqlCommand cmd=new SqlCommand("select * from userimage where id="+8 ,con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Label1.Text = dr["FileName"].ToString();
            if (!string.IsNullOrEmpty(Convert.ToString(dr["FilePath"])))
            {
              Image1.ImageUrl = "~/Ads_Images/" + Convert.ToString(dr["FilePath"]);
            }
        }
        con.Close();
    }



当我尝试通过数据阅读器进行检索时,图像也没有显示,但是当我调试代码时,它会在imageurl处获取正确的图像值。 />


文件夹(Ads_Images)存在一些问题,如果我使用文件夹(图片)并尝试存储和获取,它会将图像存储在其中但不从中提取图像图像然后它工作正常。

任何人都知道,为什么会发生???

我不想用我的Images文件夹存储所有用户图像。


The image is also not showing when i try to retreive through data reader, but when i debug code its fetching proper image value at imageurl.

There is some problem in folder(Ads_Images), It stores image in it but not fetching image from it, if i using the folder(Images) and try to store and fetch image then it works fine.
Anyone knows, why it happens???
I Dont want to store all user images with my Images folder.

推荐答案

你要保存到

you are saving to
filePath = Server.MapPath("Ads_Images/" + System.Guid.NewGuid() + fileName);



并从
加载


And Loading from

Image1.ImageUrl = "~/Ads_Images/" + Convert.ToString(dr["FilePath"]);



尝试将更改保存路径更改为


try with same like changing saving path to

filePath = Server.MapPath("~/Ads_Images/" + System.Guid.NewGuid() + fileName);


没有GUID实际上是避免命名冲突的好主意。确保避免空格。



对于相对路径,如果要从当前文件夹开始,请始终在路径前使用./。我有一些问题,我没有这样做。

所以在你的情况下:



No a GUID is actually a good idea to avoid naming collisions. Make sure to avoid spaces.

For relative paths always use ./ in front of the path if you want to start from the current folder. I´ve had issues where I didn´t do this.
so in your case:

<asp:image id="Image1" runat="server" imageurl="./Ads_Images/7f0e7ef3-4cb8-40e0-a835-00314768a70a70-515.jpg" xmlns:asp="#unknown" />
<asp:image id="Image2" runat="server" imageurl="./Images/imagenotavailable.jpg" xmlns:asp="#unknown" />





其次,Ads_Images是与图像文件夹处于同一级别的文件夹吗?

第三,如果用户将图像保存到此文件夹,则需要更新控件才能显示图像。

在您的情况下,您需要等到磁盘上存在图像并重新加载页面或调用一些ajax更新脚本来更新图像。



希望这会给你一些想法。



Secondly, is Ads_Images a folder in the same level as the Images folder?
Thirdly, if the user saves the image to this folder, the control needs to be updated in order for the image to show.
In your case you need to wait until the images exists on disk and or reload the page or call some ajax update script to update the image.

Hope this gives you some ideas.


有一些问题在Ads_Images文件夹中,我创建了一个新文件夹(Image)并尝试存储和获取图像......现在它正在工作。代码中没有问题,但我仍然不明白为什么无法访问Ads_Images文件夹图像。无论如何,现在我能够使用我的新文件夹存储和检索图像。感谢大家的回答。这个问题已经消耗了太多时间。
There is some problem in Ads_Images folder, I create a new folder(Image) and try to store and fetch images...now its working. There is no problem in the code but still i do not understand why Ads_Images folder images are not accessible. Anyway now i am able to store & retreive images with my new folder. Thanks to all for your answers. This problem already consumes too much time.


这篇关于图像控制不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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