2014-07-22 138 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的:

<!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> 

同樣的功能,當我使用ASP.NET的客戶作爲工作正常。但是我無法用Ajax/Jquery來完成,是否可以使用Ajax/JQuery?如果是,那麼如何?

任何人都可以提供一個簡單的JQuery示例嗎?

+0

您提供不夠了解1)添加參數】remotefile信息對象的詳細信息告訴你的jQuery代碼,什麼類型你用來傳遞數據的信息? –

+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,因此您必須稍微採用您的方法簽名。

[ServiceContract] 
public interface IUploadService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", 
     BodyStyle = WebMessageBodyStyle.Bare, 
     UriTemplate = "Uploadfile?fileName={filename}&length={length}")] 
    void UploadFile(string filename, int length, Stream s); 

} 

現在你有一個URL到您的文件內容發佈到。

Uploadfile?文件名= Samplefile.jpg &長度= 72572

+0

我如何使用JQuery發佈它? –