[英]Reading static files on ASP.NET
我正在使用具有多个项目的解决方案,并且在访问我当前所在的项目之外的项目时遇到问题。 这是我的代码库的一个非常简化的结构:
-ProjectA
-Pages
Index.razor
-ProjectB
- Images
-image.jpg
我在使用 Blazor Server 的 ProjectA 上,我需要读取 ProjectB 中目录外的图像。 我使用以下代码创建了一个静态文件:
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"./../ProjectB/Images/")),
RequestPath = new PathString("/MyImages")
});
问题是,当我在带有文件名的 html img 标签中使用“/MyImages”时,它工作正常,我可以查看 ProjectB 中的任何图像。 但是,当我使用 System.IO 代码时,这不起作用,例如:
@code{
File.ReadAllBytes("/MyImages/image.jpg");
}
有人可以让我知道我做错了什么吗?
编辑:我应该更清楚我的项目的目的。
我正在处理三个类, Product
、 CreateProduct
和EditProduct
。 这些类具有相同的属性,如Name, Price, etc
。 唯一的区别是Product
具有字符串ImagePath
而CreateProduct
和EditProduct
具有IFormFile Image
。
当我想创建一个新产品时,我会读取名称、价格等字符串值,然后对于图像,我使用InputFile
给我一个IBrowserFile
,对IFormFile
执行一些基本转换过程,然后将其作为CreateProduct
。 在后端,API 会将 IFormFile 存储在文件系统(又名 ProjectB)中,然后在具有 ImagePath 的 DB 中创建Product
记录。
下面是这个过程的代码片段:
[HttpPost]
public async Task<ActionResult<Product>> PostProduct([FromForm] CreateProduct model)
{
try
{
if (!ModelState.IsValid)
return BadRequest();
// This function helpers.UploadFiles will just write the file to the file system, then return the url
// For example, it'll return "/product_1/image.jpg"
var ProductImagePath = await _helpers.UploadFiles(model.Image);
var product = new Product
{
//news
Name = model.Name,
Price = model.Price,
ImagePath = ProductImagePath,
CreatedDate = DateTime.Now
};
_context.Product.Add(Product);
await _context.SaveChangesAsync();
这就是它在前端的样子:
// Converting my IBrwserFile(imageFile) to bytes so that it can be posted as IForm (Probably could do it better but that's an issue for another day)
using Stream filestream = imageFile.OpenReadStream(1024 * 1024 * 15);
using MemoryStream ms = new();
await filestream.CopyToAsync(ms);
byte[] productImage = ms.ToArray();
var formContent = new MultipartFormDataContent
{
{new StringContent("SomeProductName"),"Name"},
{new StringContent("SomeProductPrice"),"Price" },
{
new ByteArrayContent(productImage), "Image", fileName
},
};
await client.PostAsync("Product/", formContent);
我的 PUT 请求遵循相同的结构。 棘手的部分是EditProduct
场景。 我首先必须执行 GET 请求并接收只有图像路径的Product
实例。 编辑我的产品需要两种情况:
场景一:
1- Make a GET request for the `Product` class which has the image path
2- View all information including the image on the browser using <img src="ImagePath">
3- Decide to use a different image, which then I just call InputFile
4- Process InputFile and convert it to IFormFile
5- Make a PUT request and post the updated information to `EditForm` class
这很好用。 但是在场景 2 中:
1- Steps 1 & 2 as before
2- Decide not to change the image, but change other stuff like the Name and price
3- ????
现在我需要发出 PUT 请求,但由于我没有使用 InputFile,所以我没有可以发送到EditForm
类的 PUT 请求的EditForm
。 为了使 PUT 进程正常工作,我需要使用File.ReadAllBytes
读取从 ImagePath 获得的图像,然后将其读入内存并将其转换为IFormFile
。
正如我在开始时提到的,我可以使用<img src="/MyImages/@imagePath />
查看图像,其中/MyImages
是我在 Startup.cs 上创建的静态文件路径。当我将它应用到File.ReadAllBytes
或任何其他 IO 函数。
在我看来,您的两个项目是在不同的文件夹系统下编译的,但托管在同一个域下(例如,“localhost/A”和“localhost/B”)。 从浏览器的角度来看,没有问题。
但是,如果您尝试访问物理系统中的文件,则会出现问题,因为它们具有不同的根路径,并且实际上并未共享任何文件夹。 (这是应该的,因为您不希望 2 个应用程序努力控制文件系统)
为什么要直接访问图像文件? 你会以某种方式从你的其他项目中操纵它们吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.