【问题标题】:Download PDF Document using Httphandler from AngularJS使用 Httphandler 从 AngularJS 下载 PDF 文档
【发布时间】:2015-04-07 13:32:38
【问题描述】:

我开发了一个 httpHandler 来完成我使用 angularJS 的 PDF 下载。在我将服务部分移动到不同的域之前,它工作正常。当我将服务部分移动到与我的 UI AngularJS 应用程序不同的域时,它开始出现以下错误。

XMLHttpRequest 无法加载 http://localhost:57072/DocumentHandler.ashx?Caller=1&token=1。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:54470' 不允许访问。响应的 HTTP 状态代码为 500。

这是我的 HttpHandler 方法。

  public void ProcessRequest(HttpContext context)
        {

            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.AddHeader("Access-Control-Allow-Origin", "*");
            response.AppendHeader("Access-Control-Allow-Methods", "OPTIONS,POST,GET");
            response.AppendHeader("Access-Control-Allow-Headers", "X-File-Name,X-File-Type,X-File-Size");

            response.ContentType = MIMEType.Get(filExtension);
            response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

            using (FileStream stream = this.Open(filesDirectory + fileName))
            {
                byte[] buffer = new byte[10240];
                int count = 0;

                while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    response.OutputStream.Write(buffer, 0, count);
                    response.Flush();
                }
            }
        }

谁能告诉我这里出了什么问题?

【问题讨论】:

    标签: angularjs cross-domain httphandler ashx


    【解决方案1】:

    这是一个 CORS 问题。您必须配置服务器以启用跨域资源共享。这可以在服务器上的 Web.config 中完成。 CORS 在 WebAPI 2.2 中可用,您应该考虑将 HttpHandler 功能移植到返回 HttpResponseMessage 的 Web api 控制器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多