How can I fetch a list of images from an api into react

I am learning React and I learned how to fetch an image from an API. The API fetches a byte array from an sqlite database and it creates a FileContentResult. In react it fetches via the blob function(await response.blob) and it creates an url that you can use in React. So far it is pretty straightforward with no problems. The problem comes when I am trying to fetch a list of FileContentResult. What would be the most practical way of doing it? Is it to do a fetch json and get the base64 string in each of them or is there a better way of doing it? I have heard that This is the code that works with one picture. How would I modify it to work with multiple pictures.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//private static List<FileContentResult > items = new List<FileContentResult >();
[HttpGet]
public ActionResult<IEnumerable<FileContentResult>> GetPicture()
{
try
{
string query = "SELECT * FROM pictures";
using var getconnection = new SqliteConnection(@"Data Source=picturedatabase.db");
getconnection.Open();
using var command = new SqliteCommand(query, getconnection);
using var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//var id = reader.GetInt32(0);
//var filename = reader.GetString(1);
var image = (System.Byte[])reader[2];
FileContentResult imageToReact = ByteToImage(image);
return imageToReact;
}
}
}
catch { }
//This is just a placeholder, don't worry about it for now
return null;
}
public FileContentResult ByteToImage(byte[] imageBytes)
{
return File(imageBytes, "image/png");
}
</code>
<code>//private static List<FileContentResult > items = new List<FileContentResult >(); [HttpGet] public ActionResult<IEnumerable<FileContentResult>> GetPicture() { try { string query = "SELECT * FROM pictures"; using var getconnection = new SqliteConnection(@"Data Source=picturedatabase.db"); getconnection.Open(); using var command = new SqliteCommand(query, getconnection); using var reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { //var id = reader.GetInt32(0); //var filename = reader.GetString(1); var image = (System.Byte[])reader[2]; FileContentResult imageToReact = ByteToImage(image); return imageToReact; } } } catch { } //This is just a placeholder, don't worry about it for now return null; } public FileContentResult ByteToImage(byte[] imageBytes) { return File(imageBytes, "image/png"); } </code>
//private static List<FileContentResult > items = new List<FileContentResult >();

        [HttpGet]
        public ActionResult<IEnumerable<FileContentResult>> GetPicture()
        {
            try
            {
                string query = "SELECT * FROM pictures";
                using var getconnection = new SqliteConnection(@"Data Source=picturedatabase.db");
                getconnection.Open();
                using var command = new SqliteCommand(query, getconnection);
                using var reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        //var id = reader.GetInt32(0);
                        //var filename = reader.GetString(1);
                        var image = (System.Byte[])reader[2];
                        FileContentResult imageToReact = ByteToImage(image);
                        return imageToReact;
                    }
                }
            }
            catch { }

            //This is just a placeholder, don't worry about it for now
            return null;

        }


        public FileContentResult ByteToImage(byte[] imageBytes)
        {
            return File(imageBytes, "image/png");
        }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const [selectedBlob, setSelectedBlob] = useState(null);
useEffect(() => {
const fetchImage = async () => {
try {
const response = await fetch('https://localhost:7037/api/Picture');
if (response.ok) {
const data = await response.blob();
setSelectedBlob(URL.createObjectURL(data));
} else {
console.error("Error fetching image:", response.statusText);
}
} catch (error) {
console.error("Error fetching image:", error);
}
};
fetchImage();
}, []);
</code>
<code>const [selectedBlob, setSelectedBlob] = useState(null); useEffect(() => { const fetchImage = async () => { try { const response = await fetch('https://localhost:7037/api/Picture'); if (response.ok) { const data = await response.blob(); setSelectedBlob(URL.createObjectURL(data)); } else { console.error("Error fetching image:", response.statusText); } } catch (error) { console.error("Error fetching image:", error); } }; fetchImage(); }, []); </code>
const [selectedBlob, setSelectedBlob] = useState(null);


useEffect(() => {
    const fetchImage = async () => {
        try {
            const response = await fetch('https://localhost:7037/api/Picture');
            if (response.ok) {
                const data = await response.blob();
                setSelectedBlob(URL.createObjectURL(data));
            } else {
                console.error("Error fetching image:", response.statusText);
            }
        } catch (error) {
            console.error("Error fetching image:", error);
        }
    };

    fetchImage();
}, []);

I tried to fetch the list with response.blob and that just creates a blob of the list.

New contributor

cooolsson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật