微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用src属性

如何解决如何使用src属性

我在Azure上托管了一个API,该API比较两个pdf文件生成一个新的结果pdf。我想使用embed标签在html网页中打开结果pdf。当我将kudu文件url放在embed标签的src属性中时,由于安全原因,它无法打开。 当我使用src =“ username:password@testpdfcomparison.scm.azurewebsites.net/api/vfs/site/wwwroot/pdf/Output.pdf”时,我在浏览器中收到此错误

其URL包含嵌入式凭据(例如https://user:pass@host/)的子资源请求被阻止。有关更多详细信息,请参见https://www.chromestatus.com/feature/5669008342777856

实际上,我想以静登录方式打开此pdf文件。我可以通过jquery或c#

通过任何其他方式执行此操作吗

这是pdf文件链接https://testpdfcomparison.scm.azurewebsites.net/api/vfs/site/wwwroot/pdf/Output.pdf

解决方法

我不知道您的.pdf文件路径为何包含scm

无论您的程序使用哪种语言代码,生成的pdf文件(存储在当前操作环境下的某个文件夹中)都必须使用相对路径。(建议在使用Azure存储的情况下使用有很多文件)

azure webapp中的文件路径包含scm,因此必须执行授权验证。例如,您提供的文档链接,我的帐户无权访问。如下所示。

enter image description here

实际上,azure应用程序服务本质上是iis。当我们编写代码时,需要存储文件。建议使用relative paths。文件路径如下,我的示例代码为.net core 3.0

enter image description here

public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"Html")),RequestPath = "/Html"
        });
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"PDF")),RequestPath = "/PDF"
        });
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"api")),RequestPath = "/api"
        });
        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

您可以download my sample code并进行部署。您可以访问下面两个网址。

  1. https://yourwebsitename.azurewebsites.net/Html/a.html
  2. https://yourwebsitename.azurewebsites.net/api/vfs/site/wwwroot/pdf/testpdf.pdf

enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。