JAINIK PATEL
JAINIK PATEL

Reputation: 23

large file send using webservice in asp.net c#?

I have written following code and it does not work. The following error comes while uploading file to the web services:

1.An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full

2.The underlying connection was closed: An unexpected error occurred on a send.

I have used the following code for the web service and when the file size is more than 90 mb the error comes:

LocalService.IphoneService obj = new LocalService.IphoneService();
byte[] objFile = FileToByteArray(@"D:\Brijesh\My Project\WebSite5\IMG_0010.MOV");
int RtnVal = obj.AddNewProject("demo", "demo", "[email protected]", "[email protected]", 1, 2,    29, "IMG_0010.MOV", objFile,"00.00.06");

public byte[] FileToByteArray(string fileName)
{
    byte[] fileContent = null;
    System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
    long byteLength = new System.IO.FileInfo(fileName).Length;
    //byteLength = 94371840;
    fileContent = binaryReader.ReadBytes((Int32)byteLength);
    fs.Close();
    fs.Dispose();
    binaryReader.Close();
    return fileContent;
}

Upvotes: 2

Views: 4240

Answers (1)

nunespascal
nunespascal

Reputation: 17724

No socket will transfer 200MB in one chunk. Your will receive data in chunks mostly be between 1024 and 4096 bytes(depending on your settings).

  1. Read this data in chunks.
  2. Reassemble your file on the server.
  3. Then use this received file, assembled from bytes, as you need.

For an asp.net webservice:

enable webservice to receive large amounts of data

Increase the ASP.NET limits on the maximum size of SOAP messages and the maximum number of seconds that a request is allowed to execute by adding the configuration element to the application's web.config file. The following code example sets the ASP.NET limit on the maximum size of an incoming request to 400MB and the maximum amount of time a request is allowed to execute to 5 minutes (300 seconds).

Put this in your web.config.

<configuration>
  <system.web>
  <httpRuntime maxMessageLength="409600"
    executionTimeoutInSeconds="300"/>
  </system.web>
</configuration>

Remember you will be blocking a thread for as long this request is processed. This will not scale for a large number of users.

Upvotes: 1

Related Questions