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以上的大文件的

附上測試工程項目源碼:http://files.cnblogs.com/easywebfactory/WcfService1.rar

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM