从服务器数据库使用WCF上载/下载大文件 [英] Upload/Download large file using WCF from server database
问题描述
我已经创建了Win32应用程序使用的WCF服务。我已经添加了一个服务来通过WCF服务上传和下载大文件,以传输我正在使用byte []数组的文件。因为我的文件存储在数据库中。我传递的是具有以下数据字段的类对象。
public long FileID;
public string FileName;
public string FileMIME;
public byte [] FileStream;
还有更多字段,它们是字符串类型,但它们与上传文件无关。
该服务的问题是我无法上传超过16kb的文件大小,但是我可以下载100kb +的文件。我已经搜索了很多关于这个问题并修改了我的web.config和app.config文件如下。
web.config文件
<? xml version = 1.0 ?>
< 配置 >
< ; connectionStrings >
< add name = ITR1_Entity 连接 string = connection string; MultipleActiveResultSets = True providerName = System.Data.EntityClient / >
< add 名称 = CAIS_Entity connectionString = 连接字符串 ; MultipleActiveResultSets < span class =code-keyword> = True providerName = System.Data.EntityClient / > ;
< / connectionStrings >
< system.web >
< 编译 debug = true targetFramework = 4.0 / >
< httpRuntime maxRequestLength = 2147483647 / >
< / system.web >
< ; system.serviceModel >
< bindings >
< basicHttpBinding >
< binding < span class =code-attribute> name = BasicHttpBinding_ICAIS__AdvisiorWebService closeTimeout = 01:10:00
< span class =code-attribute> openTimeout = 01:10:00 receiveTimeout = 01:10:00 sendTimeout = 01: 10:00
allowCookies = false bypassProxyOnLocal = false hostNameCo mparisonMode = StrongWildcard
< span class =code-attribute> maxBufferSize = 2147483647 maxBufferPoolSize = 2147483647 maxReceivedMessageSize = 2147483647
< span class =code-attribute> messageEncoding = 文字 textEncoding = utf-8 transferMode = 缓冲
useDefaultWebProxy = true >
< readerQuotas maxDepth = 2147483647 maxStringContentLength = 2147483647 < span class =code-attribute> maxArrayLength = 2147483647
maxBytesPerRead = 2147483647 maxNameTableCharCount = 2147483647 / >
< security mode = 无 >
< transport clientCredentialType = 无 proxyCredentialType = 无
realm = / >
< 消息 clientCredentialType = UserName algorithmSuite = 默认 / >
< / security >
< / binding >
< / basicHttpBinding >
< < span class =code-leadattribute> / bindings >
< 客户 >
< endpoint name = BasicHttpBinding_ICAIS__AdvisiorWebService 地址 = http:// localhost:1964 / CAIS_AdvisiorWebService.svc
< span class =code-attribute> binding = basicHttpBinding
bindingConfiguration = BasicHttpBinding_ICAIS__AdvisiorWebService
合同 = ICAIS_AdvisiorWebService >
< / endpoint >
< endpoint name = BasicHt tpBinding_IITR1_WebService 地址 =
binding = basicHttpBinding
bindingConfiguration = BasicHttpBinding_ITR1_WebService
< span class =code-attribute> 合同 = IITR1_WebService >
< / endpoint >
< / client >
< 行为 >
< endpointBehaviors >
< 行为 名称 = customeBehavior >
< dataContractSerializer maxItemsInObjectGraph = 2147483647 / >
< /行为跨度> >
< / endpointBehaviors >
< serviceBehaviors >
< 行为 >
<! - 为避免泄露元数据信息,请将以下值设置为false并在部署之前删除上面的元数据端点 - >
< serviceMetadata httpGetEnabled = true / >
<! - 为了调试目的,在故障中接收异常详细信息,将下面的值设置为true。在部署之前设置为false以避免泄露异常信息 - >
< serviceDebug includeExceptionDetailInFaults = true / >
< dataContractSerializer maxItemsInObjectGraph = 2147483647 / >
< / behavior >
< / serviceBehaviors >
< / behavior < span class =code-keyword>>
< serviceHostingEnvironment multipleSiteBindingsEnabled = true / >
< / system.serviceModel >
< system.webServer >
< modules runAllManagedModulesForAllRequests = true / >
< / system.webServer >
< / configuration >
这是我的app.config文件
< ? xml version = 1.0 encoding = utf-8 ?>
< configuration >
< connectionStrings >
< add name = DataConnection connectionString = 数据源=。\ SQL2K8;初始目录= HnR_1.0;集成安全性=真 providerName = System.Data.SqlClient / >
< / connectionStrings >
< system.serviceModel >
< bindings >
< basicHttpBinding >
< binding name = BasicHttpBinding_IITR1_WebService closeTimeout = 10:01:00
< span class =code-attribute> openTimeout = 10:01:00 receiveTimeout = 10:01:00 sendTimeout = 10:01:00
allowCookies=\"false \" bypassProxyOnLocal=\" false\" hostNameComparisonMode=\"StrongWildcard\"
maxBufferSize=\"2147483647\" maxBufferPoolSize=\"2147483647\" maxReceivedMessageSize=\"2147483647\"
messageEncoding=\"Text\" textEncoding=\"utf-8\" transferMode=\"Buffered\"
useDefaultWebProxy=\"true\">
<readerQuotas maxDepth=\"32\" maxStringContentLength=\"2147483647\" maxArrayLength=\"2147483647\"
maxBytesPerRead=\"2147483647\" maxNameTableCharCount=\"2147483647\" />
<security mode=\"None\">
<transport clientCredentialType=\"None\" proxyCredentialType=\"None\"
realm=\"\" />
<message clientCredentialType=\"UserName\" algorithmSuite=\"Default\" />
</security>
</binding>
<binding name=\"BasicHttpBinding_ICAIS__AdvisiorWebService\" closeTimeout=\"10:01:00\"
openTimeout=\"10:01:00\" receiveTimeout=\"10:01:00\" sendTimeout=\"10:01:00\"
allowCookies=\"false\" bypassProxyOnLocal=\"false\" hostNameComparisonMode=\"StrongWildcard\"
maxBufferSize=\"2147483647\" maxBufferPoolSize=\"2147483647\" maxReceivedMessageSize=\"2147483647\"
messageEncoding=\"Text\" textEncoding=\"utf-8\" transferMode=\"Buffered\"
useDefaultWebProxy=\"true\">
<readerQuotas maxDepth=\"32\" maxStringContentLength=\"2147483647\" maxArrayLength=\"2147483647\"
maxBytesPerRead=\"2147483647\" maxNameTableCharCount=\"2147483647\" />
<security mode=\"None\">
<transport clientCredentialType=\"None\" proxyCredentialType=\"None\"
realm=\"\" />
<message clientCredentialType=\"UserName\" algorithmSuite=\"Default\" />
</security>
</binding>
<binding name=\"BasicHttpBinding_ICAIS_ManagerWebService1\" closeTimeout=\"10:01:00\"
openTimeout=\"10:01:00\" receiveTimeout=\"10:01:00\" sendTimeout=\"10:01:00\"
allowCookies=\"false\" bypassProxyOnLocal=\"false\" hostNameComparisonMode=\"StrongWildcard\"
maxBufferSize=\"2147483647\" maxBufferPoolSize=\"2147483647\" maxReceivedMessageSize=\"2147483647\"
messageEncoding=\"Text\" textEncoding=\"utf-8\" transferMode=\"Buffered\"
useDefaultWebProxy=\"true\">
<readerQuotas maxDepth=\"32\" maxStringContentLength=\"2147483647\" maxArrayLength=\"2147483647\"
maxBytesPerRead=\"2147483647\" maxNameTableCharCount =\"2147483647\" />
<security mode=\"None\">
<transport clientCredentialType=\"None\" proxyCredentialType=\"None\"
realm=\"\" />
<message clientCredentialType=\" UserName\" algorithmSuite=\"Default\" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client >
<endpoint address=\"http://localhost:1964/ITR1_WebService.svc\"
binding=\"basicHttpBinding\" bindingConfiguration=\"Bas icHttpBinding_IITR1_WebService\"
contract=\"WebServiceITR1.IITR1_WebService\" name=\"BasicHttpBinding_IITR1_WebService\" />
<endpoint address=\"http://localhost:1964/CAIS_AdvisiorWebService.svc\"
binding=\"basicHttpBinding\" bindingConfiguration=\"BasicHttpBinding_ICAIS__AdvisiorWebService\"
contract=\"WebServiceEmpLogin.ICAIS__AdvisiorWebService\" name=\"BasicHttpBinding_ICAIS__AdvisiorWebService\" />
<endpoint address=\"http://localhost:1964/CAIS_ManagerWebService1.svc\"
binding=\"basicHttpBinding\" bindingConfiguration=\"BasicHttpBinding_ICAIS_ManagerWebService1\"
contract=\"WebServiceManager.ICAIS_ManagerWebService1\" name=\"BasicHttpBinding_ICAIS_ManagerWebService1\" />
</client>
</system.serviceModel>
<startup>
<supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\" />
</startup>
</configuration>
the service \"WebServiceEmpLogin.ICAIS__AdvisiorWebService\" has that function which ll let my client upload and download the file.
please help me to upload and download lar ge file with wcf service.
Thank you
You increase the timeout period in web.config
<system.web>
<httpRuntime executionTimeout=\"240\" maxRequestLength=\"20480\" />
</system.web>
pls refer these links it may help you..
http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx[^]
Send large file to WCF service and vise versa[^]
WCF Streaming: Upload/Download Files Over HTTP[^]
@sixthplanets: thank you for help. I have gone through all this article and after that i have modified my web.config and app.config a bit.
i.e.: i have changed mytransferMode=\"Buffered\" to transferMode=\"Streamed\"
and this throws an error for bad request 400
here i am sharing my WCF function with you
public CAIS_FileAttachmentInfoBO downloadfile(long FileID)
var objAttachment_DAL = objEntity_DAL.FileAttachment1
.Where(us => us.FileID== FileID)
.Select(us => new
{
us.FileID,
us.FileMIME,
us.FileName,
us.FileStream
})
.FirstOrDefault();
CAIS_FileAttachmentInfoBO objFileAttachmentBO =
new CAIS_FileAttachmentInfoBO();
if (objAttachment_DAL != null && objAttachment_DAL.FileID > 0)
{
objFileAttachmentBO.FileName = objAttachment_DAL.FileName;
objFileAttachmentBO.FileStream = objAttachment_DAL.FileStream;
objFileAttachmentBO.Is_Password = false;
objFileAttachmentBO.Password = \"\";
objFileAttachmentBO.FileID = objAttachment_DAL.FileID;
}
return objFileAttachmentBO;
here \"CAIS_FileAttachmentInfoBO\" is my class which has the 5 data field as used in function.
I don’’t think that the problem with my function
thank you. :)
Hi, I have created a WCF services which is used by Win32 application. I have added a service to upload and download the large file via WCF services, to transfer a file I am using byte[] array. because my files are stored in the database. I am passing an object of class which have following data fields.
public long FileID; public string FileName; public string FileMIME; public byte[] FileStream;
and few more field which are type of String but they are not related with Uploading file.
The problem with the service is that i''m not able to upload file size more than 16kb, however i am able to download a file of 100kb+. I have googled a lot about this problem and modified my web.config and app.config file as follow.
web.config file
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="ITR1_Entity" connection string=""connection string";MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> <add name="CAIS_Entity" connectionString="connection string";MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime maxRequestLength="2147483647" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_ICAIS__AdvisiorWebService" closeTimeout="01:10:00" openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint name="BasicHttpBinding_ICAIS__AdvisiorWebService" address="http://localhost:1964/CAIS_AdvisiorWebService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICAIS__AdvisiorWebService" contract="ICAIS_AdvisiorWebService"> </endpoint> <endpoint name="BasicHttpBinding_IITR1_WebService" address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITR1_WebService" contract="IITR1_WebService"> </endpoint> </client> <behaviors> <endpointBehaviors> <behavior name="customeBehavior"> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior> <!--To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment--> <serviceMetadata httpGetEnabled="true" /> <!--receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information--> <serviceDebug includeExceptionDetailInFaults="true" /> <dataContractSerializer maxItemsInObjectGraph="2147483647"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> </configuration>
here is my app.config file
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="DataConnection" connectionString="Data Source=.\SQL2K8;Initial Catalog=HnR_1.0;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IITR1_WebService" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> <binding name="BasicHttpBinding_ICAIS__AdvisiorWebService" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> <binding name="BasicHttpBinding_ICAIS_ManagerWebService1" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:1964/ITR1_WebService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IITR1_WebService" contract="WebServiceITR1.IITR1_WebService" name="BasicHttpBinding_IITR1_WebService" /> <endpoint address="http://localhost:1964/CAIS_AdvisiorWebService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICAIS__AdvisiorWebService" contract="WebServiceEmpLogin.ICAIS__AdvisiorWebService" name="BasicHttpBinding_ICAIS__AdvisiorWebService" /> <endpoint address="http://localhost:1964/CAIS_ManagerWebService1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICAIS_ManagerWebService1" contract="WebServiceManager.ICAIS_ManagerWebService1" name="BasicHttpBinding_ICAIS_ManagerWebService1" /> </client> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> </configuration>
the service "WebServiceEmpLogin.ICAIS__AdvisiorWebService" has that function which ll let my client upload and download the file.
please help me to upload and download large file with wcf service.
Thank you解决方案You increase the timeout period in web.config
<system.web> <httpRuntime executionTimeout="240" maxRequestLength="20480" /> </system.web>
pls refer these links it may help you..
http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx[^]
Send large file to WCF service and vise versa[^]
WCF Streaming: Upload/Download Files Over HTTP[^]
@sixthplanets: thank you for help. I have gone through all this article and after that i have modified my web.config and app.config a bit.
i.e.: i have changed mytransferMode="Buffered" to transferMode="Streamed"
and this throws an error for bad request 400
here i am sharing my WCF function with you
public CAIS_FileAttachmentInfoBO downloadfile(long FileID) var objAttachment_DAL = objEntity_DAL.FileAttachment1 .Where(us => us.FileID== FileID) .Select(us => new { us.FileID, us.FileMIME, us.FileName, us.FileStream }) .FirstOrDefault(); CAIS_FileAttachmentInfoBO objFileAttachmentBO = new CAIS_FileAttachmentInfoBO(); if (objAttachment_DAL != null && objAttachment_DAL.FileID > 0) { objFileAttachmentBO.FileName = objAttachment_DAL.FileName; objFileAttachmentBO.FileStream = objAttachment_DAL.FileStream; objFileAttachmentBO.Is_Password = false; objFileAttachmentBO.Password = ""; objFileAttachmentBO.FileID = objAttachment_DAL.FileID; } return objFileAttachmentBO;
here "CAIS_FileAttachmentInfoBO" is my class which has the 5 data field as used in function.
I don''t think that the problem with my function
thank you. :)
这篇关于从服务器数据库使用WCF上载/下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!