2012-04-11 4 views
1

나는 간단한 웹 서비스를 가지고 있고, 하나의 텍스트 파일을 돌려 줄 방법을 만들고 싶다. 나는 이런 식으로했다 :Windows Azure에서 실행되는 WCF 서비스에서 파일을 반환하는 방법은 무엇입니까?

public byte[] GetSampleMethod(string strUserName) 
    { 
     CloudStorageAccount cloudStorageAccount; 
     CloudBlobClient blobClient; 
     CloudBlobContainer blobContainer; 
     BlobContainerPermissions containerPermissions; 
     CloudBlob blob; 
     cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; 
     blobClient = cloudStorageAccount.CreateCloudBlobClient(); 
     blobContainer = blobClient.GetContainerReference("linkinpark"); 
     blobContainer.CreateIfNotExist(); 
     containerPermissions = new BlobContainerPermissions(); 
     containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; 
     blobContainer.SetPermissions(containerPermissions); 
     string tmp = strUserName + ".txt"; 
     blob = blobContainer.GetBlobReference(tmp); 
     byte[] result=blob.DownloadByteArray(); 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment; filename="+strUserName + ".txt"); 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; 
     WebOperationContext.Current.OutgoingResponse.ContentLength = result.Length; 
     return result; 
    } 

... 그리고 서비스 인터페이스 :

[OperationContract(Name = "GetSampleMethod")] 
    [WebGet(UriTemplate = "Get/{name}")] 
    byte[] GetSampleMethod(string name); 

그리고 그것은 나에게 XML 응답을 포함하는 테스트 파일을 반환합니다. 질문 : XML 직렬화없이 파일을 반환하려면 어떻게해야합니까?

+0

서비스에 연결하고 파일을 다운로드하기 위해 사용하는 클라이언트는 무엇입니까? Visual Studio에 의해 생성 된 클라이언트는 바이트 배열로 자동으로 deserialize됩니다. –

+0

브라우저에서 작동해야합니다. –

+0

브라우저에서 파일을 첨부 파일로 저장하거나 콘텐츠를 표시 하시겠습니까? –

답변

7

대신 방법을 변경하여 Stream을 반환하십시오. 또한 반환하기 전에 전체 내용을 바이트 []로 다운로드하지 않을 것을 제안합니다. 대신 Blob에서 스트림을 반환하십시오. 나는 당신의 방법을 적용하려고 시도했지만, 이것은 자유형 코드이므로 컴파일하지 않거나 그대로 실행되지 않을 수 있습니다.

public Stream GetSampleMethod(string strUserName){ 
    //Initialization code here 

    //Begin downloading blob 
    BlobStream bStream = blob.OpenRead(); 

    //Set response headers. Note the blob.Properties collection is not populated until you call OpenRead() 
    WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition", "attachment; filename="+strUserName + ".txt"); 
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; 
    WebOperationContext.Current.OutgoingResponse.ContentLength = blob.Properties.Length; 

    return bStream; 
} 
관련 문제