我使用此命令为照片上传位置创建了一个文件夹。
public class ApplicationRootSiteData : IApplicationRootSiteData
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IHttpContextAccessor _httpContextAccessor;
public ApplicationRootSiteData(IHostingEnvironment hostingEnvironment, IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
}
public string RootFolderName => "Upload";
public string ProductPictureFolder => "ProductPictureFolder";
public string ProductMainPictureFolder => "ProductMainPicture";
public string WebRootPath => _hostingEnvironment.WebRootPath;
public Uri GetAbsoluteUri()
{
var request = _httpContextAccessor.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = request.Scheme;
uriBuilder.Host = request.Host.Host;
uriBuilder.Path = request.Path.ToString();
uriBuilder.Query = request.QueryString.ToString();
return uriBuilder.Uri;
}
public string RootPath => Path.Combine(WebRootPath, RootFolderName);
public string GetProductPicturePath()
{
return Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder);
}
public string GetProductMainPicturePath()
{
string path = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
return path;
}
public string GetNewPath()
{
string productMainPicturePath = GetProductMainPicturePath();
return Path.Combine(productMainPicturePath);
}
}
[HttpPost("UploadProductPic"), DisableRequestSizeLimit]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult UploadProductPic()
{
var file = Request.Form.Files[0];
try
{
if (!Directory.Exists(_applicationRoot.GetNewPath()))
{
if (!Directory.Exists(_applicationRoot.RootPath))
{
Directory.CreateDirectory(_applicationRoot.RootPath);
}
if (!Directory.Exists(_applicationRoot.GetProductPicturePath()))
{
Directory.CreateDirectory(_applicationRoot.GetProductPicturePath());
}
if (!Directory.Exists(_applicationRoot.GetProductMainPicturePath()))
{
Directory.CreateDirectory(_applicationRoot.GetProductMainPicturePath());
}
}
if (file.Length > 0)
{
string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
string fullPath = Path.Combine(_applicationRoot.GetNewPath(), file.Name);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
它可以工作并创建一个文件夹并在其中上传图片。
现在我需要在Angular (Front-End )
中显示图像,但这会显示此错误:
不允许加载本地资源:file:/// C:/Users/Mr-Programer/Desktop/New%20folder/StoreFinal/StoreFinal/wwwroot/Upload/ProductPictureFolder/ProductMainPicture/77777.png
并且我在asp核心中使用此URL:https://localhost:44390/wwwroot/Upload/ProductPictureFolder/ProductMainPicture/77777.png
这是我的启动程序:
app.UseHttpsRedirection();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseStaticFiles();
app.UseDefaultFiles();
var scopeFactory = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
}
现在有什么问题?我该如何解决?
答案 0 :(得分:0)
动态添加图像时,它们不再是“静态”文件。
您需要通过控制器来提供这些服务。
[HttpGet("/img/{id}/{name}")]
public FileStreamResult GetImage(string id, string path){
var myPathToFile = Path.Combine(_rootPath, id, path);
var stream = new FileStream(myPathToFile, FileMode.Open, FileAccess.Read);
return new FileStreamResult(stream ,$"image/{path.Split('.')[1]}")
}