【发布时间】:2020-06-04 00:40:05
【问题描述】:
我在 asp net core 中有一个 web api:
[HttpGet("{id}")]
public async Task<ActionResult> GetFileSystemStoreObject(string id)
{
var archivo = await _context.FileSystemStoreObject.FindAsync(id);
if (archivo == null)
{
return NotFound();
}
...
var image = System.IO.File.OpenRead(rutaArchivo);
return File(image, "image/jpeg");
}
我想从我的反应应用程序中显示图像。但我不知道该怎么做。 我已经尝试了以下反应,但它不起作用。
function NoticiaFoto({url}) {
const [error, setError] = useState(null);
const [isLoaded, setIsLoaded] = useState(false);
const [item, setItem] = useState([]);
let ruta = 'https://localhost:44357/api/filesystemstoreobjects/' + url;
useEffect(() => {
fetch(ruta)
.then(res => res.json())
.then(
(result) => {
setIsLoaded(true);
setItem(result);
},
(error) => {
setIsLoaded(false);
setError(error);
}
)
}, [])
return(
<div><img src={item} alt= "foto" /></div>
)
}
export default NoticiaFoto
我已经测试了 API,它运行良好。
我不知道如何从反应中获取图像并将其显示在屏幕上。
请帮忙。
【问题讨论】:
-
尝试将img元素的height和width属性设置为某个值
-
fetch 的“结果”数据是什么? base64、arrayBuffer 等...
标签: reactjs