異常

2015-10-25 57 views
-1

我用this example和我的代碼是異常

private void dialogBtn_Click(object sender, EventArgs e) 
{ 
    string file; 
    DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. 
    if (result == DialogResult.OK) // Test result. 
    { 
     file = openFileDialog1.FileName; 

     System.IO.FileInfo fileInfo = new System.IO.FileInfo(file); 
     TransferServiceClient clientUpload = new TransferServiceClient(); 
     RemoteFileInfo uploadRequestInfo = new RemoteFileInfo(); 

     using (System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
     { 
      uploadRequestInfo.FileName = file; 
      uploadRequestInfo.Length = fileInfo.Length; 
      uploadRequestInfo.FileByteStream = stream; 
      //clientUpload.UploadFile(uploadRequestInfo); 
      clientUpload.UploadFile(fileInfo.Name, fileInfo.Length, stream);      
     }     
    }   
} 

在收到錯誤的clientUpload.UploadFile(fileInfo.Name, fileInfo.Length, stream);

類型的未處理的異常「System.ServiceModel.FaultException`1」發生在mscorlib.dll 附加信息:找不到路徑'C:\ upload \ Book1.xlsx'的一部分。

但是,Book1.xlsx不在上傳文件夾中。它在桌面上。

回答

1

好了,本例中的UploadFile方法的服務實現如下:

public void UploadFile(RemoteFileInfo request) 
{ 
    FileStream targetStream = null; 
    Stream sourceStream = request.FileByteStream; 

    string uploadFolder = @"C:\upload\"; 

    string filePath = Path.Combine(uploadFolder, request.FileName); 

    using (targetStream = new FileStream(filePath, FileMode.Create, 
          FileAccess.Write, FileShare.None)) 
    { 
     //read from the input stream in 65000 byte chunks 

     const int bufferLen = 65000; 
     byte[] buffer = new byte[bufferLen]; 
     int count = 0; 
     while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0) 
     { 
      // save to output stream 
      targetStream.Write(buffer, 0, count); 
     } 
     targetStream.Close(); 
     sourceStream.Close(); 
    } 
} 

注行

string uploadFolder = @"C:\upload\"; 

您需要更改到您想要的一些現有的文件夾上傳的文件爲,存儲爲

+0

謝謝我做到了!我在服務機器上創建了一個上傳文件夾,但爲什麼我需要在我的客戶機上上傳文件夾? – Ctrlfreak

+1

@George你不需要它在客戶端機器上。只有在您正在調試時(在同一臺機器上同時運行客戶端和服務器)。請注意,您正在接收故障異​​常,這意味着錯誤來自服務。 –