生成的HTML IMG无法显示 - 提供的路径不正确(?)

时间:2015-12-22 13:09:23

标签: c# html asp.net

我正在尝试根据用户在特定日期上传到网站的图片数量来编写动态生成“图库”的代码。 这些图片存储在名为UploadedPictures的目录内的VS Project文件夹(ImageProject)中。此目录包含每个月和该月所有日期的子目录。

当用户点击某个按钮时。应填充此图库。

protected void InsertAttachments(int ID)
    {
        HtmlGenericControl Div = new HtmlGenericControl("div");
        HtmlGenericControl Img = new HtmlGenericControl("img");
        try
        {
            string[] ImageArray = Directory.GetFiles(Server.MapPath("~/UploadedPictures"), "*_*_" + ID + "_*.jpeg", SearchOption.AllDirectories);
            try
            {
                foreach (var Picture in ImageArray)
                {
                    Div = new HtmlGenericControl("Div");
                    Div.Attributes["class"] = "col-lg-3 col-md-4 col-xs-6 thumb";

                    Img = new HtmlGenericControl("img");
                    Img.Attributes["class"] = "img-responsive";
                    Img.Attributes.Add("src", Picture);

                    IMGContainer.Controls.Add(Div);
                    Div.Controls.Add(Img);
                }
            }
            catch (Exception)
            {
            }
        }
        catch (Exception)
        {
        }
    }

从代码中可以看出,我正在尝试根据找到的图片数量生成新的HTML元素。然后将这些HTML元素添加到IMGContainer Div。

单击按钮后的结果 - 生成以下HTML代码,无法看到任何内容。

<div class="col-lg-3 col-md-4 col-xs-6 thumb">
    <img class="img-responsive" src="C:\inetpub\wwwroot\ImageProject\ImageProject\
    UploadedPictures\2015_12\2015_12_22\5_1_1213_1.jpeg">
</div>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
    <img class="img-responsive" src="C:\inetpub\wwwroot\ImageProject\ImageProject\
    UploadedPictures\2015_12\2015_12_22\5_1_1213_2.jpeg">
</div>

问题是,即使提供的路径正确也没有显示图片,图片也不会存储在项目文件夹外部..

此外,这个提供的路径在测试VM服务器和工作环境之外都是可见的,例如,如果我使用家用PC访问该网站。

在这两种情况下,这是我在检查浏览器控制台时收到的错误消息:

Not allowed to load local resource:    
file:///C:/inetpub/wwwroot/ImageProject/ImageProject/UploadedPictures/2015_12/2015_12_22/5_1_1213_1.jpeg

有没有人知道我做错了什么?

3 个答案:

答案 0 :(得分:1)

您的服务器代码正在生成图像的本地路径,因此当您将代码发布到互联网并且客户端浏览器呈现该页面时,它会获得加载他无法访问的图像的指示。

您必须将创建的网址更改为相对于托管项目,因此任何外部客户端都可以访问它们。

答案 1 :(得分:1)

只需使用当前目录参考

protected void InsertAttachments(int ID)
{
    HtmlGenericControl Div = new HtmlGenericControl("div");
    HtmlGenericControl Img = new HtmlGenericControl("img");
    try
    {
        string[] ImageArray = Directory.GetFiles(Server.MapPath("./UploadedPictures"), "*_*_" + ID + "_*.jpeg", SearchOption.AllDirectories);
        try
        {
            foreach (var Picture in ImageArray)
            {
                Div = new HtmlGenericControl("Div");
                Div.Attributes["class"] = "col-lg-3 col-md-4 col-xs-6 thumb";

                Img = new HtmlGenericControl("img");
                Img.Attributes["class"] = "img-responsive";
                Img.Attributes.Add("src", Picture);

                IMGContainer.Controls.Add(Div);
                Div.Controls.Add(Img);
            }
        }
        catch (Exception)
        {
        }
    }
    catch (Exception)
    {
    }
}

答案 2 :(得分:0)

@Override
    protected void onDraw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();

        Point a = new Point(0, 0);
        Point b = new Point(width / 2, height);
        Point c = new Point(width, 0);

        Path trianglePath = new Path();
        trianglePath.moveTo(a.x, a.y);
        trianglePath.lineTo(b.x, b.y);
        trianglePath.lineTo(c.x, c.y);
        trianglePath.lineTo(a.x, a.y);

        Paint paint = new Paint();
        paint.setStrokeWidth(1);
        paint.setColor(Color.parseColor("#7A7A7A"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        canvas.drawPath(trianglePath, paint);

        Paint textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(height / 6);
        textPaint.setTypeface(TypefaceUtils.getTypeface(getContext(), TypefaceUtils.AVAILABLE_FONTS.ROBOTO_THIN));

        String receive = getContext().getString(R.string.receive_password).split("\n")[0];
        String password = getContext().getString(R.string.receive_password).split("\n")[1];
        Rect receiveBounds = new Rect();
        Rect passwordBounds = new Rect();
        textPaint.getTextBounds(receive, 0, receive.length(), receiveBounds);
        textPaint.getTextBounds(password, 0, password.length(), passwordBounds);

        canvas.drawText(
                receive,
                (width - receiveBounds.width()) / 2,
                height / 3 - receiveBounds.height() + receiveBounds.height() / 5,
                textPaint);
        canvas.drawText(
                password,
                (width - passwordBounds.width()) / 2,
                height / 3 + receiveBounds.height(),
                textPaint);

        canvas.scale(1, 1);
    }