将文件上传到WCF [英] Uploading files to wcf

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

问题描述

我需要使用流媒体上传文件.当用户在浏览器中选择文件时,该文件将转换为流
如下所示.


客户端代码:

I need to upload files using streaming. when a user selected a file in browser it is converted to stream
is is done as follows.


client side code:

if (FileUpload1.HasFile)
        { 
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
            FileTrasferServiceReference.TransferServiceClient clientUpload = new TransferServiceClient();
            using (System.IO.FileStream stream = new System.IO.FileStream(FileUpload1.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {                
                clientUpload.UploadFile(stream); 
            }
        } 



Wcf服务端代码:



Wcf Service side code:

public void UploadFile(System.IO.Stream FileByteStream)
    {
        FileStream targetStream = null;
        Stream sourceStream =  FileByteStream;

        string uploadFolder = @"C:\upload\";
        //string filename = request.FileMetaData.Filename;
        string filePath = Path.Combine(uploadFolder, "xyz.zip");

        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            //read from the input stream in 4K chunks
            //and save to output stream
            const int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
                targetStream.Write(buffer, 0, count);
            }
            targetStream.Close();
            sourceStream.Close();
        }

    } 



文件未上传,以上代码正确还是错误?



the files are not being uploaded, is the above code correct or wrong? if not correct can some one provide sample code of it.

推荐答案

我建​​议您将try/catch块放在所有内容周围,然后在调试器下运行它. br/>
可能是您的WCF连接未建立.
I recommend that you put try/catch blocks around everything, and then run it under the debugger.

It might be that your WCF connection isn''t being established.


这是在评论中发布评论的同一个人,我无法正确添加代码,所以我在这里添加.

我已经建立了wcf连接
服务器端绑定
===================
this is the same person who posted the comment in the comment a above i was not able add the code properly so i am adding it here.

I have established the wcf connection
server side binding
===================
binding name="TransferService"
                 transferMode="Streamed"
                 messageEncoding="Text"
                 maxReceivedMessageSize="2147483647">
             <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
                 maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
             <security mode="None">
               <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
               <message clientCredentialType="UserName" algorithmSuite="Default" />
             </security>
           </binding







behavior name="TransferServiceBehavior">
                 <serviceMetadata httpGetEnabled="true" />
               <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                 <serviceDebug includeExceptionDetailInFaults="true" />
             </behavior





客户端绑定
==================





Client side binding
==================


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

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