如何在ASP.NET Core中访问bin文件夹外的文件?

huangapple go评论57阅读模式
英文:

How to access files in outside of bin folder in ASP.NET Core?

问题

我正在开发一个 ASP.net Core MVC 的 Web 应用程序,尝试访问项目中名为 TempFiles 的文件夹中的 TempFile。

MyWebAPP:.
├───TempFiles
     ├─── file.wav

但我无法通过 JavaScript 访问该文件,即使它存在,代码如下:

const connection = new signalR.HubConnectionBuilder().withUrl("/SandboxHub").build();

// 开始 SignalR 连接
connection.start().catch(err => console.error(err));

// 处理文本框更改事件
$("#sentence-input").on("input", async function () {
    // 获取句子的值
    const sentence = $(this).val();

    // 将句子发送到 Hub
    await connection.invoke("TransformIntoTTS", sentence);

    connection.on("ReceiveFile", function (fileUrl) {
        const relativeFilePath = fileUrl.replace("/LearningWithAI-WebLayer", "");

        // 将文件 URL 应用于音频控制器的源
        $("#audio-player").attr("src", "../" + relativeFilePath);
    });
});
英文:

I am developing a web app in ASP.net Core MVC and am trying to access a TempFile in a folder called TempFile within my project.

MyWebAPP:.
├───TempFiles
     ├─── file.wav

However I cannot access that file, even tho it exists, through JavaScript, like this:

const connection = new signalR.HubConnectionBuilder().withUrl("/SandboxHub").build();

// Start the SignalR connection
connection.start().catch(err => console.error(err));

// Handle textbox change event
$("#sentence-input").on("input", async function () {
    // Get the sentence value
    const sentence = $(this).val();

    // Send the sentence to the Hub
    await connection.invoke("TransformIntoTTS", sentence);

    connection.on("ReceiveFile", function (fileUrl) {
        const relativeFilePath = fileUrl.replace("/LearningWithAI-WebLayer", "");


        // Apply the file URL to the source of the audio controller
        $("#audio-player").attr("src", "../" + relativeFilePath);

    });
});

答案1

得分: 2

我建议您尝试在wwwroot文件夹内创建文件夹,而不是使用项目的默认Web根文件夹,因为只有asp.net core中的wwwroot文件夹可以被客户端访问。

如果您想在Web根之外提供文件,您可以尝试使用以下代码:

using Microsoft.Extensions.FileProviders;

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
           Path.Combine(builder.Environment.ContentRootPath, "TempFiles")),
    RequestPath = "/TempFiles"
});

有关asp.net core中静态文件的详细信息,您可以参考这篇文章

英文:

I suggest you could try to create the folder inside the wwwroot folder instead of using the project's default web root folder, since only wwwroot folder inside the asp.net core could be accessed by the client.

If you want to serve files outside of web root, you should try to use below codes:

using Microsoft.Extensions.FileProviders;

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
           Path.Combine(builder.Environment.ContentRootPath, "TempFiles")),
    RequestPath = "/TempFiles"
});

More details about how static files works inside asp.net core, you could refer to this article.

huangapple
  • 本文由 发表于 2023年5月22日 16:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定