如何从角度4的asp.net web api加载图像?

时间:2018-05-17 14:17:12

标签: angular asp.net-web-api2

我从角度4上传图像并将其存储在我的web api项目的文件夹中。如何从后端获取这些图像?这是将其存储在下面的web api中的代码。

[HttpPost]
    [Route("api/SaveFoodItemImage")]
    [AllowAnonymous]
    public HttpResponseMessage SaveFoodItemImage()
    {
        string imageName = null;
        var httpRequest = HttpContext.Current.Request;         
        var postedFile = httpRequest.Files["Image"];         
        imageName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
        imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
        var filePath = HttpContext.Current.Server.MapPath("~/Image/" + imageName);
        postedFile.SaveAs(filePath);
        var uploadedImageId = httpRequest["FoodItemId"];           
        var foodItem = _context.FoodItems.FirstOrDefault(p => p.Id == uploadedImageId);
        foodItem.FoodItemImage = imageName;
        _context.SaveChanges();
        return Request.CreateResponse(HttpStatusCode.Created);
    }

如何在前端的对象中检索它们?

0 个答案:

没有答案