【问题标题】:file upload download using WCF rest文件上传下载使用 WCF 休息
【发布时间】:2013-10-14 16:03:37
【问题描述】:

我正在使用 .net 4.0 并尝试使用名为 upload 的方法使用 rest wcf 服务上传文件,并使用名为 download 的方法使用相同的 rest 服务下载文件。有一个控制台应用程序将流数据发送到 wcf 服务,并且从该服务下载相同的控制台应用程序。控制台应用程序使用 WebChannelFactory 连接并使用服务。

这是来自控制台应用程序的代码

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

这是 wcf 服务代码

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

现在,在我检查文件的这一步,我注意到它与从控制台应用程序发送的原始文件具有相同的千字节大小,但是当我打开文件时,它没有内容。该文件可以是任何文件类型,无论是 Excel 还是 Word 文档,它仍然作为空文件出现在服务器上。所有的数据都去哪儿了?

我做fileStream.ReadByte()>0 的原因是因为我在某处读到,当通过网络传输时,无法找到缓冲区长度(这就是为什么我在尝试获取流的长度时遇到异常的原因服务upload 方法)。这就是为什么我不使用byte[] buffer = new byte[32768]; 的原因,如许多字节数组固定的在线示例所示。

从服务下载时,我在 wcf 服务端使用此代码

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

在控制台应用程序方面我有

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

现在这个下载的文件也是空白的,因为它下载的是我之前使用上面的上传代码上传的文件,即使上传的文件大小为 200 KB,它的大小也只有 16 KB。

我的代码有什么问题?任何帮助表示赞赏。

【问题讨论】:

    标签: c# .net wcf rest


    【解决方案1】:

    你可以让它变得更简单:

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

    您现有代码的问题是您调用ReadByte 并读取整个输入流,然后尝试再次读取它(但现在已经结束了)。这意味着您的第二次读取将全部失败(read 变量在您的循环中将为 0,并立即中断),因为流现在已结束。

    【讨论】:

    • 谢谢。我会试试的。为什么你认为下载只有 16kb 而不是我刚刚上传的整个 200kb?
    • @user20358 如果您通过上述机制上传,它将生成一个 0 字节的文件。我会先修复上传,然后专注于下载部分...
    • @user20358 你也可以这样写hte download
    • 谢谢。我也这样做了。下载存在其他问题。
    【解决方案2】:

    在这里,我创建了一个 WCF REST 服务。 Service.svc.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace UploadSvc
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
                     InstanceContextMode = InstanceContextMode.PerCall,
                     IgnoreExtensionDataObject = true,
                     IncludeExceptionDetailInFaults = true)]    
          public class UploadService : IUploadService
        {
    
            public UploadedFile Upload(Stream uploading)
            {
                var upload = new UploadedFile
                {
                    FilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())
                };
    
                int length = 0;
                using (var writer = new FileStream(upload.FilePath, FileMode.Create))
                {
                    int readCount;
                    var buffer = new byte[8192];
                    while ((readCount = uploading.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        writer.Write(buffer, 0, readCount);
                        length += readCount;
                    }
                }
    
                upload.FileLength = length;
    
                return upload;
            }
    
            public UploadedFile Upload(UploadedFile uploading, string fileName)
            {
                uploading.FileName = fileName;
                return uploading;
            }
        }
    }
    

    Web.config

    <?xml version="1.0"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpRuntime executionTimeout="100000" maxRequestLength="20000000"/>
      </system.web>
      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding maxBufferSize="2147483647"
                     maxBufferPoolSize="2147483647"
                     maxReceivedMessageSize="2147483647"
                     transferMode="Streamed"
                     sendTimeout="00:05:00" closeTimeout="00:05:00" receiveTimeout="00:05:00">
              <readerQuotas  maxDepth="2147483647"
                             maxStringContentLength="2147483647"
                             maxArrayLength="2147483647"
                             maxBytesPerRead="2147483647"
                             maxNameTableCharCount="2147483647"/>
              <security mode="None" />
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="defaultEndpointBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="defaultServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="UploadSvc.UploadService" behaviorConfiguration="defaultServiceBehavior">
            <endpoint address="" behaviorConfiguration="defaultEndpointBehavior"
              binding="webHttpBinding" contract="UploadSvc.IUploadService"/>
          </service>
        </services>    
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
    
    </configuration>
    

    Consumer.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Cache;
    using System.Text;
    
    namespace UploadSvcConsumer
    {
        class Program
        {
            static void Main(string[] args)
            {
                //WebClient client = new WebClient();
                //client.UploadData("http://localhost:1208/UploadService.svc/Upload",)
                //string path = Path.GetFullPath(".");
                byte[] bytearray=null ;
                //throw new NotImplementedException();
                Stream stream =
                    new FileStream(
                        @"C:\Image\hanuman.jpg"
                        FileMode.Open);
                    stream.Seek(0, SeekOrigin.Begin);
                    bytearray = new byte[stream.Length];
                    int count = 0;
                    while (count < stream.Length)
                    {
                        bytearray[count++] = Convert.ToByte(stream.ReadByte());
                    }
    
                string baseAddress = "http://localhost:1208/UploadService.svc/";
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + "Upload");
                request.Method = "POST";
                request.ContentType = "text/plain";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;
                Stream serverStream = request.GetRequestStream();
                serverStream.Write(bytearray, 0, bytearray.Length);
                serverStream.Close();
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    int statusCode = (int)response.StatusCode;
                    StreamReader reader = new StreamReader(response.GetResponseStream());
    
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多