使用WCF其余文件上传下载 [英] file upload download using WCF rest

查看:105
本文介绍了使用WCF其余文件上传下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 4.0和尝试使用一个方法叫做上传休息WCF服务上传文件,并使用用一个叫做下载方法相同REST服务下载。这里是发送数据流的WCF服务,从服务相同的控制台应用程序下载一个控制台应用程序。控制台应用程序使用WebChannelFactory连接,并与服务工作。

I am using .net 4.0 and trying to upload a file using a rest wcf service with a method called upload and download it using the same rest service using a method called download. There is a console application that sends the stream data to the wcf service and the same console app downloads from the service. The console application uses WebChannelFactory to connect and work with the service.

下面是从控制台应用程序中的code

Here is a code from the console app

            StreamReader fileContent = new StreamReader(fileToUpload, false);
            webChannelServiceImplementation.Upload(fileContent.BaseStream );

这是WCF服务code

This is the wcf service code

public void Upload(Stream fileStream)
{
        long filebuffer  = 0;
        while (fileStream.ReadByte()>0)
        {
            filebuffer++;
        }

        byte[] buffer = new byte[filebuffer];
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int read;

            while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                memoryStream.Write(buffer, 0, read);

            }
            File.WriteAllBytes(path, buffer);
        }
}

现在,在我检查文件这一步,我注意到它具有相同的大小以KB为单位,将其用的控制台应用程序发送的原始文件,但是当我打开这个文件,它没有任何内容。该文件可以是任何类型的文件,无论是Excel或Word文档,但它仍然过来对服务器作为一个空文件。哪里是所有的数据到哪里去了?

Now at this step when I examine the file, I notice it has the same size in Kilobytes as the original file that was sent from the console app, but when I open the file, it has no content. The file can be any file type, be it an excel or word document, it still comes up to the server as an empty file. Where is all the data gone?

我做的原因 fileStream.ReadByte()> 0 是因为我读的地方,transfering在网络上时,缓冲区的长度不能被发现(这就是为什么我一直想把在服务上传法)流的长度时,得到一个例外。这就是为什么我不使用字节[]缓冲区=新的字节[32768]; 如所示在字节数组是固定的许多在线实例

The reason I am doing a fileStream.ReadByte()>0 is because I read somewhere that when transfering over the network, the buffer length cant be found out (which is why I was getting an exception when trying to get the length of the stream in the service upload method). This is why I dont use byte[] buffer = new byte[32768]; as in shown in many online examples where the byte array is fixed.

虽然从我用这个code在WCF服务端的下载服务。

While downloading from the service I use this code on the wcf service side

    public Stream Download()
    {
        return new MemoryStream(File.ReadAllBytes("filename"));
    }

而在控制台应用程序端我有

And on the console app side I have

        Stream fileStream = webChannelServiceImplementation.Download();

        //find size of buffer
        long size= 0;
        while (fileStream .ReadByte() > 0)
        {
            size++;
        }
        byte[] buffer = new byte[size];

        using (Stream memoryStream = new MemoryStream())
        {
            int read;
            while ((read = fileContents.Read(buffer, 0, buffer.Length)) > 0)
            {
                memoryStream.Write(buffer, 0, read);
            }

            File.WriteAllBytes("filename.doc", buffer);
        }

现在这个下载的文件也是空白的,因为它的下载大小我上传的早期使用上述上传code中的文件,它只有16 KB,即使上传的文件为200 KB的大小。

Now this downloaded file is also blank, because its downloading the file I uploaded earlier using the upload code above, and its only 16 KB in size even though the uploaded file was 200 KB in size.

什么是错我的code?任何帮助AP preciated。

What is wrong with my code? Any help appreciated.

推荐答案

您可以让这个更简单:

public void Upload(Stream fileStream)
{
    using (var output = File.Open(path, FileMode.Create))
        fileStream.CopyTo(output);
}

与您现有的code的问题是,你叫 ReadByte 并读取整个输入流,然后尝试再次读取(但它现在在最后)。这意味着你的第二个读操作将全部失败(即变量将是0在你的循环,并立即打出来),因为数据流现在是在其结束。

The problem with your existing code is that you call ReadByte and read the entire input stream, then try to read it again (but it's now at the end). This means your second reads will all fail (the read variable will be 0 in your loop, and immediately break out) since the stream is now at its end.

这篇关于使用WCF其余文件上传下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆