簡體   English   中英

wcf 傳輸文件之前的異常

[英]Exception before a wcf transfer file

我使用了這個例子,我的代碼是

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); 我收到一個錯誤

mscorlib.dll 中出現類型為“System.ServiceModel.FaultException`1”的未處理異常附加信息:找不到路徑“C:\\upload\\Book1.xlsx”的一部分。

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

那么,示例中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\";

您需要將其更改為要存儲上傳文件的某個現有文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM