简体   繁体   English

从javascript发送消息到处理程序

[英]send message from javascript to handler

I have client code running in javascript which tries to send html content to a custom handler. 我有在javascript中运行的客户端代码,该代码尝试将html内容发送到自定义处理程序。

The client side code looks like this: 客户端代码如下所示:

   ...
    $('#txt_content').val($(div).html());
        performClick($('#submit_excel_form'));

And the handler code is: 处理程序代码为:

public void ProcessRequest(HttpContext context)
        {
            string name = context.Request["txt_name"];
            string content = context.Request["txt_content"];
           ...
        }

The problem is that when using Chrome browser, the message the handler get (txt_content) is not complete. 问题是使用Chrome浏览器时,处理程序获取(txt_content)的消息不完整。 Maximum length of string I get is: 524288 我得到的最大字符串长度是:524288

When I run in explorer , I get the full message (with length=567130). 在资源管理器中运行时,我收到完整的消息(长度为567130)。

I saw a similar quesion here , but it was not answered (The solution with setting maxAllowedContentLength doesn't work). 我在这里看到了类似的问题,但没有得到答复(设置maxAllowedContentLength的解决方案不起作用)。

Would appreciate any help. 将不胜感激。 Thanks, Omer 谢谢,Omer

Edit: 编辑:

This is the relevant part from the configuration file: 这是配置文件中的相关部分:

<webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    <customErrors mode="Off" defaultRedirect="Default.aspx">
      <error statusCode="500" redirect="Default.aspx"/>
      <error statusCode="404" redirect="404.aspx"/>
    </customErrors>
    <globalization culture="he-IL" uiCulture="he-IL" resourceProviderFactoryType="BusinessLogic.Culture.CultureProviderFactory" enableClientBasedCulture="false"/>
    <compilation debug="true">
      <assemblies>
        <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>
    </compilation>
    <httpRuntime requestValidationMode="2.0" maxRequestLength="2097151" requestLengthDiskThreshold="8192000"/>
    <authentication mode="Forms"/>
    <pages clientIDMode="Static" validateRequest="false">
      <controls>
        <add tagPrefix="amr" namespace="BusinessLogic.Controls" assembly="BusinessLogic"/>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
      <namespaces>
        <add namespace="Infrastructure"/>
        <add namespace="CommonData"/>
      </namespaces>
    </pages>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="OptionInfer" value="true"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <remove name="ScriptModule"/>
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated"/>
      <remove name="ScriptHandlerFactory"/>
      <remove name="ScriptHandlerFactoryAppServices"/>
      <remove name="ScriptResource"/>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
  </system.webServer>

You can likely get around this limit/bug with a multipart post. 您可以通过分篇文章来解决此限制/错误。

A full, but complicated, example of doing exactly this in a .ashx can be found at: How to post data from a webform page to an HTTPHandler.ashx file? 在.ashx中执行此操作的完整但复杂的示例可以在以下位置找到: 如何将数据从Web表单页面发布到HTTPHandler.ashx文件? This injects a file uploader that then multipart posts the "File" data into the handler. 这将注入文件上传器,然后将“文件”数据分段发送到处理程序中。 Messy. 凌乱。

You may be able to do this more straightforwardly, by combining the multipart xhr.send from XMLHttpRequest POST multipart/form-data with a method similar to the first part of the answer possibly using BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream); 通过将XMLHttpRequest POST multipart / form-data的多部分xhr.send与类似于答案第一部分的方法结合使用,可能可以使用BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);

or 要么

   //read content into a buffer
   request.Content.LoadIntoBufferAsync().Wait();

   request.Content.ReadAsStringAsync().ContinueWith(t =>
   {
       apiRequest.Content = t.Result;
       _repo.Add(apiRequest);
   });

from Multipart form POST using ASP.Net Web API 使用ASP.Net Web API多部分表单POST中获取

or even potentially the .NET 4.5 async from how to read multi part form data in .net web api controller 甚至可能是.NET 4.5 async如何.net Web API控制器中读取多部分表单数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM