简体   繁体   English

wcf 传输文件之前的异常

[英]Exception before a wcf transfer file

I used this example and my code is我使用了这个例子,我的代码是

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);                      
        }                
    }           
}

During the clientUpload.UploadFile(fileInfo.Name, fileInfo.Length, stream);clientUpload.UploadFile(fileInfo.Name, fileInfo.Length, stream); I receive an error我收到一个错误

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll Additional information: Could not find a part of the path 'C:\\upload\\Book1.xlsx'. mscorlib.dll 中出现类型为“System.ServiceModel.FaultException`1”的未处理异常附加信息:找不到路径“C:\\upload\\Book1.xlsx”的一部分。

But, the Book1.xlsx is not inside upload folder.但是,Book1.xlsx 不在上传文件夹中。 It's at the Desktop.它在桌面上。

Well, the service implementation of the UploadFile method in the example is as follows:那么,示例中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();
    }
}

Note the line注意行

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

You need to change that to some existing folder where you want the uploaded files to be stored .您需要将其更改为要存储上传文件的某个现有文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM