2013-05-14 2 views
1

응용 프로그램 "A"는 POST를 사용하여 워드 파일을 [바이트 배열로] 외부 응용 프로그램에 업로드해야합니다.HttpPost를 사용하여 서버에 문서 업로드

파일 본문은 요청 본문에 명명 된 매개 변수로 추가되어야하며 파일 업로드를 위해 POST 요청을해야합니다.

나는 샘플 코드가 있지만 java에있다. 동일한 C# 코드를 작성하고 싶습니다. 하지만 C#에서는 MultiPartEntity와 비슷한 객체를 찾을 수 없었습니다.

자바 코드 :

String restURL = HOSTURL + "/rest/upload/0b002f4780293c18";   
String fileName = "testRestUploadByFolderID" + Calendar.getInstance().getTimeInMillis() + ".txt";   
File testFile = createNewFile("C:/Temp/rest/" + fileName);   
FileBody content = new FileBody(testFile, "application/octet-stream");   
System.out.println(" File Name : " + content.getFilename() + " ... "    +  content.getTransferEncoding());   
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);   
reqEntity.addPart("filename", new StringBody(fileName));   
reqEntity.addPart("uploadfile", content);   
HttpPost httpPost = new HttpPost(restURL);   
httpPost.addHeader("Accept", "application/json");   
httpPost.setEntity(reqEntity);     

// Post the request   
String response = httpclient.execute(httpPost, new DefaultResponseHandler()); 

당신이

감사합니다 포함한 FileContent을 업로드 C#으로 명명 된 매개 변수를하는 방법에 대해 설명합니다 몇 가지 링크를 게시하시기 바랍니다 수 없습니다. link.It 아래에

답변

1

을 통해 이동 자바 언어 변환 도구 (JLCA)의 도움을 취할 수 멀티 파트 콘텐츠 게시물을 찾으려면

참고 :

이그 일의 .NET 4.5 비동기 방식,하지만 당신은 너무 일부 Nuget 패키지 설치 .NET 4에서이 솔루션을 사용할 수 있습니다

을 코드 :

using (HttpClient httpClient = new HttpClient()) 
using (var multiPartContent = new MultipartFormDataContent()) 
{ 

    httpClient.BaseAddress = new Uri(BaseAddress); 

    var fileContent = new ByteArrayContent(*filebytes*); 

    //Create content header 
    fileContent.Headers.ContentDisposition = new ontentDispositionHeaderValue("attachment") 
       { 
        FileName = *fileName* 
       }; 

     //Add file to the multipart request 
     multiPartContent.Add(fileContent); 

     //Add any other file? 
     ... 


     //Post it 
     HttpResponseMessage response = await httpClient.PostAsync("hostURL", multiPartContent); 

} 

이것은 IM입니다. 닷넷에 그것을하는 가장 좋은 방법, 더러운에 대해 잊지 HttpRequests

관련 문제