Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
this is my question and below is my code, i want to show images .iam able to get file but not able to show images.

pls help me out

What I have tried:

[Route("GetPics")]
        [HttpGet]
        public IActionResult GetPics()
        {
            //List<Files> images = new List<Files>();
            ////Set the Image Folder Path.
            //string path = HttpContext.Current.Server.MapPath("~/Images/");

            Files f = new Files();
            var data = from i in _context.Files
                       select i.DataFiles;
       

            return Ok(data);
        }


and

<script type="text/javascript">

    $("#btnShow").click(function () {
        var options = {};
        options.url = "/api/Pics/GetPics";
        options.type = "GET";
        options.dataType = "json";
        options.contentType = "application/json";
        options.success = function (results) {
            $("#imgContainer").empty();
            for (var i = 0; i < results.length; i++) {
                debugger;
                //alert(results);
                //$("#imgContainer").html(results);
                $("#imgContainer").append("<img src='" + "api/Pics/GetPics/" + results[i].type + "' /> <br />");
                //$('#imgContainer').attr('src', results[i].target.result);
            }
        };
        options.error = function (err) { alert(err.statusText); };
        $.ajax(options);
    });


    
</script>


and

[Table("Files")]
    public class Files
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int DocumentId { get; set; }
        [MaxLength(100)]
        public string Name { get; set; }
        [MaxLength(100)]
        public string FileType { get; set; }
        [MaxLength]
        public byte[] DataFiles { get; set; }
        public DateTime? CreatedOn { get; set; }
    }


and
CREATE TABLE [dbo].[Files] (
    [DocumentId] INT             IDENTITY (1, 1) NOT NULL,
    [Name]       VARCHAR (100)   NULL,
    [FileType]   VARCHAR (100)   NULL,
    [DataFiles]  VARBINARY (MAX) NULL,
    [CreatedOn]  DATETIME        NULL,
    CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED ([DocumentId] ASC)
);
Posted
Updated 29-Apr-23 18:24pm
v2
Comments
Member 15627495 11-Apr-23 1:07am    
it seems you're close to achieve.

if all images don't appear it's a 'path error'
use JS with "console.log(var datas)" or "console.table(var datas)" to check the images values,
check the paths are well formed.
Raja 22S 15-Apr-23 3:27am    
hi,
can u send the code dude

thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900