【发布时间】:2020-12-14 19:08:18
【问题描述】:
我正在尝试通过 wcf 服务将文件从 react 应用程序上传到服务器。该代码在本地服务器上运行良好。该服务托管在 godaddy 服务器上。当我尝试上传文件时,服务器给出错误 400 错误请求。该代码在本地服务器上运行良好,并创建文件夹并将文件保存在正确的文件夹中。我应该提供哪些其他详细信息。
public void UploadFile(Stream stream)
{
MultipartParser parser = new MultipartParser(stream);
string uploadpath = HttpContext.Current.Server.MapPath("~/images/");
if (parser.Success)
{
string filename = HttpUtility.UrlDecode(parser.Filename);
File.WriteAllBytes(uploadpath + filename, parser.FileContents);
}
else
{
Console.WriteLine("error");
}
}
}
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
<trust level="Full" originUrl=""/>
<customErrors mode="Off"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehaviors">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web" >
<webHttp></webHttp>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="largeMessage" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="mailService.Service1" behaviorConfiguration="serviceBehaviors">
<endpoint address="" contract="mailService.IService1" binding="webHttpBinding" bindingConfiguration="largeMessage" behaviorConfiguration="web"></endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
<security>
</security>
</system.webServer>
</configuration>
文件大小不大,只有127kb
邮递员显示拒绝访问路径。
【问题讨论】:
-
看来stream应该是调用uploadfile方法的唯一参数。如何包含参数也请帮忙。
-
我已经更新了没有参数的代码。仍然给出 400 bad request 错误。
-
详细错误是什么,是跨域引起的吗?
-
400 错误请求。服务的其余部分正在运行,因此它不是跨源错误。
-
400错误的原因太多了。有详细的错误信息吗?
标签: c# wcf file-upload upload