2015-12-10 74 views
0

我目前正在使用C#和WCF的Web服務。我需要做一個上傳方法,所以我遵循this tutorial。現在我可以通過表單上傳文件和文件上傳的文件夾中,但在瀏覽器中它總是返回我有一個錯誤(像在阿賈克斯指定)這裏的所有代碼:WCF/C#上傳文件

IWebService.cs

[ServiceContract] 
public interface IWebService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")] 
    void UploadFile(string fileName, Stream stream); 
} 

WebService.svc

public void UploadFile(string fileName, Stream stream) 
{ 
    string FilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer/Uploads"), fileName); 

    int length = 0; 
    using (FileStream writer = new FileStream(FilePath, FileMode.Create)) 
    { 
     int readCount; 
     var buffer = new byte[8192]; 
     while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      writer.Write(buffer, 0, readCount); 
      length += readCount; 
     } 
    } 
} 

TestClient.html

<head> 
<script type="text/javascript"> 
    function UploadFile() { 
     fileData = document.getElementById("fileUpload").files[0]; 
     var data = new FormData(); 

     $.ajax({ 
      url: 'http://localhost:1945/Service1.svc/UploadFile?fileName=' + fileData.name, 
      type: 'POST', 
      data: fileData, 
      cache: false, 
      dataType: 'json', 
      processData: false, // Don't process the files 
      contentType: "application/octet-stream", // Set content type to false as jQuery will tell the server its a query string request 
      success: function (data) { 
       alert('successful..'); 
      }, 
      error: function (data) { 
       alert('Some error Occurred!'); 
      } 
     }); 
    } 

</script> 
<title></title> 
</head> 
<body> 
<div> 
    <div> 
     <input type="file" id="fileUpload" value="" /> 
     <br /> 
     <br /> 
     <button id="btnUpload" onclick="UploadFile()"> 
      Upload 
     </button> 
    </div> 
</div> 
</body> 

回答

2

我在試圖解決我的路

  1. 更改retun型UploadCustomFile方法字符串的問題所在,並返回一個假消息

    公共字符串UploadCustomFile(字符串文件名,System.IO.Stream流) { string FilePath = Path.Combine(HostingEnvironment.MapPath(「〜/ FileServer/Uploads」),fileName);加入

    int length = 0; 
    using (FileStream writer = new FileStream(FilePath, FileMode.Create)) 
    { 
        int readCount; 
        var buffer = new byte[8192]; 
        while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0) 
        { 
         writer.Write(buffer, 0, readCount); 
         length += readCount; 
        } 
    } 
    
        try 
        { 
         //do something else 
        } 
        catch 
        {  
          //to return custom error message - ajax will catch this as a error 
          HttpContext.Current.Response.Write(""); 
          return "not okey"; 
        } 
    
    return "okey"; 
    } 
    
  2. 更改接口方法ResponseFormat = WebMessageFormat.Json和字符串返回類型

    [OperationContract] 
    [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")] 
    String UploadCustomFile(string fileName, Stream stream); 
    

當我試圖這樣,我得到了成功警報消息

發送自定義服務器端的例外

+0

感謝您的評論它幫助我很多。您是否知道我需要爲錯誤添加回報? – PokeRwOw