2014-06-09 2 views
0

WCF를 사용하여 pdf/파일을 업로드하려고합니다.WCF 업로드 파일이 작성되지 않았습니다.

내 문제는, 내가 업로드 한 파일이

누군가가 나를 도울 수 완료되지 않은됩니다

이 제 기능 업로드 :

public string UploadFile(FileUploadMessage request) 
{ 
    Stream fileStream = null; 
    Stream outputStream = null; 
    try 
    { 
     fileStream = request.FileByteStream; 

     string rootPath = @"C:\WCF"; 

     DirectoryInfo dirInfo = new DirectoryInfo(rootPath); 

     if (!dirInfo.Exists) 
     { 
      dirInfo.Create(); 
     } 
     string newFileName = Path.Combine(rootPath, Guid.NewGuid() + ".pdf"); 
     outputStream = new FileInfo(newFileName).OpenWrite(); 
     const int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     int bytesRead = fileStream.Read(buffer, 0, bufferSize); 

     while (bytesRead > 0) 
     { 
      outputStream.Write(buffer, 0, bufferSize); 
      bytesRead = fileStream.Read(buffer, 0, bufferSize); 
     } 
     return newFileName; 
    } 
    catch (IOException ex) 
    { 
     throw new FaultException<IOException>(ex, new FaultReason(ex.Message)); 
    } 
    finally 
    { 
     if (fileStream != null) 
     { 
      fileStream.Close(); 
     } 
     if (outputStream != null) 
     { 
      outputStream.Close(); 
     } 
    } 
} 

이 내 구성입니다

<binding name="BasicHttpBinding_ITransferService" closeTimeout="04:01:00" 
        openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" 
        allowCookies="false" bypassProxyOnLocal="false" 
        hostNameComparisonMode="StrongWildcard" 
        maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 
        maxReceivedMessageSize="2147483647" 
        messageEncoding="Mtom" textEncoding="utf-8" 
        transferMode="Streamed" 
        useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="128" 
         maxStringContentLength="2147483647" maxArrayLength="2147483647" 
         maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
        <security mode="None"> 
         <transport clientCredentialType="None" 
           proxyCredentialType="None" realm="" /> 
         <message clientCredentialType="UserName" algorithmSuite="Default" /> 
        </security> 
       </binding> 
+0

나는 WCF 관련 코드를 볼 수 없습니다. – Matt

+0

파일 크기와 관련이있을 수 있습니다. WCF 구성 파일에 설정된 할당량을 확인하십시오. – MaxSC

답변

0

바이트 [] (예 : 파일)를 WCF 운영 체제로 스트리밍하려는 경우 on 연산은 Stream 타입의 매개 변수를 하나만 가져야합니다. 문제는 수신 측 (서비스)에서 바이트 []의 길이를 알아야한다는 것입니다.

하나의 솔루션은 두 개의 별도의 서비스 운영 첫 번째 호출에서 길이를 저장

public void SetLength(long length) 

public void Upload(Stream stream) 

두 번째 호출에 사용되는 두 개의 통화를 할 수있다.

이 솔루션을 사용하면 구성에서 MaxRecievedMessageSize를 큰 숫자로 설정할 필요가 없습니다.

0

이 같은 (이 예 http://www.dotnetfunda.com/articles/show/2008/upload-a-file-using-aspnet-file-upload-control-and-wcf 확인)해야한다 :

class FileUploadService : IFileUploadService 

{ 공공 부울 UploadFileData (FILEDATA FILEDATA) { 부울 결과 = 거짓; 시도하십시오 { // 파일을 저장할 위치를 설정하십시오. string FilePath = Path.Combine ({일부 설정의 경로}, fileData.FileName);

 //If fileposition sent as 0 then create an empty file 
     if (fileData.FilePosition == 0) 
     { 
      File.Create(FilePath).Close(); 
     } 

     //Open the created file to write the buffer data starting at the given file position 
     using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) 
     { 
      fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin); 
      fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length); 
     } 
    } 
    catch (Exception ex) 
    {    
     //throw FaultException<>(); 
    } 

    return result; 
} 

}

관련 문제