使用WCF进行Fileupload

时间:2018-05-29 08:01:40

标签: c# wcf file-upload

我正在尝试使用wcf服务上传图像或任何其他类型的文件,例如excel文件。我收到错误,不允许端点,或者如果我尝试达到方法错误,则说不允许使用此方法。我的代码如下。还请指导如何在地址栏中使用它。

如果我尝试使用localhost:51027 / UploadFileService.svc访问该服务,则表示端点未找到localhost的相同错误:51027 / UploadFileService.svc / uploadfile / book1.xlsx

Webconfig

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="UploadedFiles" value="Upload"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" executionTimeout="240" maxUrlLength="2097151" maxRequestLength="2097151" maxQueryStringLength="2097151"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFUploadService.UploadFileService" behaviorConfiguration="default">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration ="RestBinding"
                  name="UploadFileService" contract="WCFUploadService.IUploadFileService"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="default">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="RestBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" 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"/>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.28.0" newVersion="2.2.28.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

UploadFileService.svc

public class UploadFileService : IUploadFileService
{
    public bool UploadFile(Stream file)
    {
        string uploadDirectory = System.AppDomain.CurrentDomain.BaseDirectory + @"\" + System.Configuration.ConfigurationManager.AppSettings["UploadedFiles"];
        try
        {
            byte[] data = null;
            if (file != null)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            if (!Directory.Exists(uploadDirectory))
                Directory.CreateDirectory(uploadDirectory);
            File.WriteAllBytes(uploadDirectory + @"\" + DateTime.Now.ToString("hhmmss") + ".jpg", data);
            return true;
        }
        catch (Exception ex) 
        {
            StreamWriter sw = new StreamWriter(uploadDirectory + @"\Error.txt");
            sw.WriteLine(ex.Message);
            sw.Close();
            sw.Dispose();
            return false;
        }
    }
}

IUploadFileService.cs

public interface IUploadFileService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        bool UploadFile(Stream file);
    }

0 个答案:

没有答案