Receive and return file to / from C # WebService

How to create a WebService in C # that will receive and then return the file at the same time in one call (synchronous). What I'm trying to do is create a WebService that will receive an MS Office document, convert this document to PDF and then return this file back to the caller (in my case, I use Java for the client)

+4
source share
4 answers

As Means said in his comment, the best option is to receive and return an array of bytes into your web service.

You can upload the file as bytearray using a method like this:

public byte[] FileToByteArray(string _FileName) { byte[] _Buffer = null; try { System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); long _TotalBytes = new System.IO.FileInfo(_FileName).Length; _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes); _FileStream.Close(); _FileStream.Dispose(); _BinaryReader.Close(); } catch (Exception _Exception) { Console.WriteLine("Exception caught in process: {0}", _Exception.ToString()); } return _Buffer; } 

In addition, if you have deployed webservice as a WCF service, you may need to configure some settings to increase the amount of information you can send and the timeout. This is a sample binding configuration that allows this. (Only sample, may not meet your needs)

  <binding name="WebServiceBinding" closeTimeout="00:02:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:02:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 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> 
+2
source

you can encode small amounts of binary data to a base64 string.

0
source

here are some tutorials from different sources, it also depends on whether you use wcf or asmx. I also think that you need to create two separate functions and another function that will cause both call forwarding and recovery, although you may need time to send before you can receive it.

http://support.microsoft.com/kb/318425

http://www.zdnetasia.com/create-a-simple-file-transfer-web-service-with-net-39251815.htm

https://stackoverflow.com/questions/4530045/how-to-transfer-file-through-web-service

0
source

The easiest way is to integrate the base libraries of the ASP.NET MVC3 structure into your main Web project, and then write a simple MVC controller using a single method that returns an object of type FileResult.

Scott Hanselman posted a wonderful blog post about it in a few minutes: http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx

It works great and runs in less than 3 minutes (after integrating the MVC3 environment). There is already a stackoverflow message about returning a file to MVC: How to create a file and return it via FileResult in ASP.NET MVC?

Hi,

0
source

Source: https://habr.com/ru/post/1403962/


All Articles