随笔 - 24  文章 - 0  评论 - 201  阅读 - 12万

WCF利用Stream上传大文件

转自别人的文章,学习这个例子,基本上wcf也算入门了,接口用法、系统配置都有了

本文展示了在asp.net中利用wcf的stream方式传输大文件,解决了大文件上传问题。主要是存档方便以后遇到该问题是来查阅。贴出部分代码,如果有疑惑或需要完整代码的请留言
WebForm1.aspx.cs

复制代码
protected void Button1_Click(object sender, EventArgs e)
        {
            FileData file = new FileData();
            file.filename = FileUpload1.FileName;
            file.data = FileUpload1.PostedFile.InputStream;
            GetDataService c = new GetDataService();
            c.UploadFile(file);
            Response.Write("文件传输成功!");
}
复制代码

IService1

复制代码
namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IGetDataService
    {
        [OperationContract]
        void UploadFile(FileData file);
    }
    [MessageContract]
    public class FileData
    {
        [MessageHeader]
        public string filename;
        [MessageBodyMember]
        public Stream data;
    }



}
复制代码

 

Service1.svc

复制代码
namespace WcfService1
{
    public class GetDataService : IGetDataService
    {
        public void UploadFile(FileData file)
        {
            
            FileStream fs = new FileStream("D:\\我的项目\\WcfService1\\Files\\" + file.filename, FileMode.OpenOrCreate);

            try
            {

                BinaryReader reader = new BinaryReader(file.data);

                byte[] buffer;

                BinaryWriter writer = new BinaryWriter(fs);
                long offset = fs.Length;
                writer.Seek((int)offset, SeekOrigin.Begin);

                do
                {

                    buffer = reader.ReadBytes(1024);

                    writer.Write(buffer);

                } while (buffer.Length > 0);

                fs.Close();
                file.data.Close();

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {

                fs.Close();
                file.data.Close();

            }

        }
    }
复制代码


 

web.config

复制代码
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
      <httpRuntime   maxRequestLength="40960" />
  </system.web>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IGetDataService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:52884/mex" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IGetDataService" contract="IGetDataService"
                name="BasicHttpBinding_IGetDataService" />
        </client>
    </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>
复制代码


看到有好几位同学想要源码的,我重新做了个测试工程,经测试只要网络支持,是可以上传几十M以上的大文件的

附上测试工程项目源码:https://files.cnblogs.com/easywebfactory/WcfService1.rar

 

posted on   亲善美  阅读(6662)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
< 2012年5月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示