Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WCF REST服务和Javascript中的流以及URL参数上载文件_Javascript_C#_Rest_Wcf - Fatal编程技术网

使用WCF REST服务和Javascript中的流以及URL参数上载文件

使用WCF REST服务和Javascript中的流以及URL参数上载文件,javascript,c#,rest,wcf,Javascript,C#,Rest,Wcf,我有一个WCF REST服务,这里是实现 [WebInvoke(Method = "POST", UriTemplate="uploadFile/{filename}/{email}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [OperationContract] string uploadFile

我有一个WCF REST服务,这里是实现

 [WebInvoke(Method = "POST",
            UriTemplate="uploadFile/{filename}/{email}",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
 [OperationContract]
 string uploadFile(string filename, string email, Stream _Stream)


  public string uploadFile(string filename, string email, Stream _Stream )
  {
    // Code to save file with the given filename    
  }
上传文件的Javascript实现

  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<title></title>
<script type="text/javascript">

    function uploadBlobOrFile(blobOrFile) {

        alert("upload called123");
        alert(blobOrFile.size);

        var xhr = new XMLHttpRequest();

        var url = https://localhost/Myservice/service.svc/upload/filename/emailid ;

        xhr.open('POST', url , false);

        xhr.onload = function (e) {
            progressBar.value = 0;
            progressBar.textContent = progressBar.value;
        };

        // Listen to the upload progress.
        var progressBar = document.querySelector('progress');
        xhr.upload.onprogress = function (e) {
            if (e.lengthComputable) {
                progressBar.value = (e.loaded / e.total) * 100;
                progressBar.textContent = progressBar.value; // Fallback.
            }
        };
        xhr.onreadystatechange = function () {
        alert(xhr.status + "   result: " + xhr.responseText);
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert(xhr.responseText);
            }
        }; 

        xhr.send(blobOrFile);
    }

</script>
</head>
<body>
<input id="filePicker" type="file" name="Package" accept="image/*"/>
<br />
<progress min="0" max="100" value="0">0% complete</progress>
<br />
<button title="upload" 
        onclick="if (filePicker.files[0]) uploadBlobOrFile(filePicker.files[0])">
    <span>Upload</span>
</button>
</body>
</html> 
该服务根本没有调用并给我一条错误消息,上面写着405错误代码。 (注意:我已经在我的服务中启用了CORS。所有其他的服务方法都运行良好)

我的代码可能有什么问题

谢谢

这是我的web.config文件

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime maxQueryStringLength="2097150"
             maxRequestLength="2097150"
             maxUrlLength="2097150"    />
<authentication mode="None"/>
<authorization>
  <allow users="*"/>
</authorization>
</system.web>

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="webHttpBindingClient"
             closeTimeout="00:50:00"
             openTimeout="00:50:00"
             receiveTimeout="00:50:50"
             sendTimeout="00:50:00"
             allowCookies="false"
             bypassProxyOnLocal="false"
             hostNameComparisonMode="StrongWildcard"
             maxBufferSize="2147483647"
             maxBufferPoolSize="2147483647"
             maxReceivedMessageSize="2147483647"
             transferMode="Streamed"
             useDefaultWebProxy="true"
             crossDomainScriptAccessEnabled="true">

      <readerQuotas maxArrayLength="2147483647"
                    maxStringContentLength="2147483647"
                    maxDepth="2147483647"
                    maxBytesPerRead="2147483647" 
                    maxNameTableCharCount="2147483647" />
      <security mode="Transport"/>
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="ServiceBehaviour" name="Service">
    <endpoint address="" behaviorConfiguration="EndpBehaviour" binding="webHttpBinding" 
      bindingConfiguration="webHttpBindingClient" name="webHttpEndPointBinding"   
      contract="IService"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="EndpBehaviour">
      <webHttp helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"     
                             multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint crossDomainScriptAccessEnabled="true"/>
  </webHttpEndpoint>
  <webScriptEndpoint>
    <standardEndpoint crossDomainScriptAccessEnabled="true"/>
  </webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*"/>
    <add name="Access-Control-Allow-Credentials" value="true"/>
    <add name="Access-Control-Allow-Methods" value="POST, GET,OPTIONS"/>
    <add name="Access-Control-Max-Age" value="600000"/>
    <add name="Access-Control-Allow-Headers" value="content-type,Accept"/>
    <add name="Content-Type" value="application/json;charset=UTF-8"/>
    <add name="Allow" value="OPTIONS, TRACE, GET, HEAD, POST"/>
  </customHeaders>
</httpProtocol>
<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="20971520"  />
  </requestFiltering>
</security>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>

是否与最大上载大小相关

 <system.web>
    <httpRuntime executionTimeout="240" maxRequestLength="20480" />
  </system.web>

您是否在webHttpEndpoint上设置了正确的ReaderQuota。请发布RESTful端点的配置部分。maxAllowedContentLength=“20971520”在我看来似乎小于50MB,但我不确定。
 <system.web>
    <httpRuntime executionTimeout="240" maxRequestLength="20480" />
  </system.web>