2014-07-22 7 views
0

파일 업로드 기능이있는 WCF 서비스가 있습니다.WCF 서비스에 파일 게시

public void UploadFile(Stream s) 
{ 
    FileStream targetStream = null; 
    Stream sourceStream = s; 

    string uploadFolder = @"C:\upload\"; 
    string filePath = Path.Combine(uploadFolder, Guid.NewGuid().ToString()); 

    using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None)) 
    { 
     //read from the input stream in 6K chunks 
     //and save to output stream 
     const int bufferLen = 65000; 
     byte[] buffer = new byte[bufferLen]; 
     int count = 0; 

     while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) 
     { 
      targetStream.Write(buffer, 0, count); 
     } 

     targetStream.Close(); 
     sourceStream.Close(); 
    } 

} 


[ServiceContract] 
public interface ITransferService 
{ 
    [OperationContract] 
    RemoteFileInfo DownloadFile(DownloadRequest request); 

    //[OperationContract] 
    //void UploadFile(RemoteFileInfo request); 

    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/UploadFile")] 
    void UploadFile(Stream s); 
} 

그리고이 아약스/jQuery를 사용하고 있습니다 : 나는 클라이언트로 ASP 닷넷을 사용하고 때

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> 
<head profile="http://gmpg.org/xfn/11"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>AJAX File Upload - Web Developer Plus Demos</title> 
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script> 
<script type="text/javascript" src="js/ajaxupload.3.5.js" ></script> 
<link rel="stylesheet" type="text/css" href="./styles.css" /> 
<script type="text/javascript" > 
    $(function(){ 
     var btnUpload=$('#upload'); 
     var status=$('#status'); 
     new AjaxUpload(btnUpload, { 
      action: 'http://localhost:35711/webservice/TransferService.svc/UploadFile', 
      name: 'uploadfile', 
      onSubmit: function(file, ext){ 
       // if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
       // // extension is not allowed 
       // status.text('Only JPG, PNG or GIF files are allowed'); 
       // return false; 
       //} 
       status.text('Uploading...'); 
      }, 
      onComplete: function(file, response){ 
       //On completion clear the status 
       status.text(''); 
       //Add uploaded file to list 
       if(response==="success"){ 
        $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success'); 
       } else{ 
        $('<li></li>').appendTo('#files').text(file).addClass('error'); 
       } 
      } 
     }); 

    }); 
</script> 
</head> 
<body> 
<div id="mainbody" > 
     <h3>&raquo; AJAX File Upload Form Using jQuery</h3> 
     <!-- Upload Button, use any id you wish--> 
     <div id="upload" ><span>Upload File<span></div><span id="status" ></span> 

     <ul id="files" ></ul> 
</div> 

</body> 

같은 기능은 잘 작동합니다. 하지만 Ajax/Jquery로 할 수는 없지만 Ajax/JQuery를 사용할 수 있습니까? 그렇다면 어떻게?

누구나 간단한 JQuery 예제를 제공 할 수 있습니까?

+0

당신이 RemoteFile을 정보 오브젝트 세부 사항이 jQuery 코드를 말해 어떤 종류의 데이터를 전달하는 데 사용했습니다 추가) 1을 이해하는 것만으로는 충분하지가 제공 한 정보? –

+0

Jquery가 없습니다. 하지만 네, 제 질문을 편집하고 더 많은 코드를 추가했습니다. –

+0

jQuery 파일 업 로더를 사용하고 있습니까? 이것을 확인하십시오 - http://www.codeproject.com/Articles/59551/Consuming-a-WCF-ASMX-REST-Service-using-jQuery –

답변

0

jQuery의 경우 UploadService를 REST 서비스로 제공해야합니다. WCF에서는 webHttpBinding을 통해 호스팅합니다.

이 바인딩은 MessageContracts를 지원하지 않으므로 메서드 서명을 약간 채택해야합니다.

이제 파일 내용을 게시 할 URL이 있습니다.

을 UploadFile? fileName에 = Samplefile.jpg & 길이 = 72572

+0

그리고 어떻게 JQuery를 사용하여 게시합니까? –

관련 문제