开发者

Link to image database

I have the following page:

@{
    if (Request["ID"].IsInt())
    {
        var imgID = Request["ID"].AsInt();

        //Data
        var db = Database.Open("AMSDArquiteturaConnectionString");
        var image = db.QuerySingle("select * from Images where [ID] = @0", imgID);

        if (image.MimeType.StartsWith("image"))
        {
            Response.AddHeader("content-disposition", "inline; filename=" + image.Name);
        }
        else
        {
            Response.AddHeader("content-disposition", "attachment; filename=" + image.Name);
        }

        Respon开发者_如何转开发se.BinaryWrite((byte[])image.File);

    }
}

I'm using a plugin to image to zoom in, and I need to make the link point to an image.

<a rel="images" title="MyImage" href="ProjectImage?ID=@img.ID"></a>

the problem is that when I click on the link I see only a meaningless code.

How to make this link point to a picture that is in the database?


You need to establish whether the problem is with how you are using the plugin, or your Razor code. Request the page directly, hard coding a value for imgID to make sure it works properly. If you have based your database table and code on an article from a bloke called Mikesdotnetting, you can simplify it a bit by using the WebImage helper:

@{
    WebImage image = null;
    int id = Request["Id"].AsInt();
    var db = Database.Open("FileUploading");
    var sql = "Select * From Files Where FileId = @0";
    var file = db.QuerySingle(sql, id);
    if(file != null){
        image = new WebImage((byte[])file.FileContent);
    }
}   
@if(image != null){
    @image.Write(image.ImageFormat)
}

If that works, then the plugin might the problem. Try requesting the image from an img tag, with the src pointing to the page that generates the image. It's an odd approach that uses anchors to display images instead of img tags.

Or it could be the image itself. Only some image types are supported by browsers.


You might need to specify the mime type. Something like this:

Response.ContentType = image.MimeType

The "meaningless code" is probably the image, the browser just doesn't know that the data represents an image and is showing it in raw format. This code should tell it what it needs to know.


Why are you doing this in a razor page? It should be an Action method in your controller. Something like this.

public ActionResult Image(int? id) {
    if (id.HasValue) {
        //Data
        var db = Database.Open("AMSDArquiteturaConnectionString");
        var Image = db.QuerySingle("select * from Images where [ID] = @0", id.Value);


        FileContentResult result = new FileContentResult((byte[])Image.File, Image.MimeType);

        if (Image.MimeType.StartsWith("image")) {
            result.FileDownloadName = Image.Name;
        }
        return result;

    } else {
        return new HttpNotFoundResult();
    }
}

My guess would be that view engine doesn't like it when you use it that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜