Click here to Skip to main content
15,886,724 members
Articles / Web Development / ASP.NET

Uploading large files from Android to ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (7 votes)
16 Oct 2014CPOL3 min read 31K   14   7
Uploading large files from Android to ASP.NET

Table of contents 

Introduction

This article will explain how to upload large files from Android client to an ASP.NET server.

We are going to implement several methods on Android and ASP.NET, and take a look at some ASP.NET server configurations.

Android implementation

HttpResult class and HttpErros enum

First we are going to implement a simple Java class with enum which identifies the http request errors.

This is not necessary, but it will make it much easier to handle the problems on the UI, which accrue while sending the http request.

We are going to get the HttpResult as a response from our method and we’ll be able to understand not only if the message was sent, but also what exception stopped the execution.

Java
public enum HttpErrors
{
    NO_ERROR,
    ConnectTimeoutException,
    SocketTimeoutException,
    ClientProtocolException,
    IOException
}

public class HttpResult
{
    public HttpErrors Error;
    public String Result;
    
    public HttpResult(String  serverMessage, HttpErrors error)
    {
        Result = serverMessage;
        Error = error;
    }
    
    public boolean IsError()
    {
        return !Error.equals(HttpErrors.NO_ERROR);
    }
}

SendFile method

This method simply forms the URL. Well, not in this example but if you need to set some GET parameters in it, this is the place.

It identifies whether the file exist or not, and accordingly to the file size, sets the timeout of the Request by passing double value to PostFile method.

public HttpResult SendFile(File file)
{
    String url = "your_server_url";
                
    if(file.exists())
    {
        double timeout = file.length() / 1024 / 20;
        result = PostFile(file, url, (int)timeout);
    }
    
    return result;
}

PostFile method

This method is the one that sends the given file to the specified server by URL using POST method.

It’s based on the use of HttpClient , HttpPost and FileEntity SDK classes.

It initiates our httpClient object and HttpPost. Afterwards it sets the FileEntitys ContentType to “application/zip” and setting it as the entity of the request.

In the try-catch we are waiting for server response and setting it as String type to our result.

If the execution time of the request is more than our specified timeout, SocketTimeoutException exception will be thrown.

Remember that any network communication cannot be on the main Thread!

Java
public HttpResult PostFile(File file, String url, int timeout)
{
    HttpResult result = null;
    HttpClient httpclient = new DefaultHttpClient();
    
    HttpPost request = new HttpPost(url);
    request.setParams(GetHttpParams(timeout));
    FileEntity reqEntity = new FileEntity(file, "application/zip");
    request.setEntity(reqEntity);

    HttpResponse response;
    
    try
    {
        response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

        if (entity != null)
        {
            result = new HttpResult(EntityUtils.toString(entity),
                        HttpErrors.NO_ERROR);
            entity.consumeContent();
        }
    }
    catch(SocketTimeoutException timeout)
    {
        result = new HttpResult(null,HttpErrors.SocketTimeoutException);
    }
    catch (ClientProtocolException e)
    {
        result = new HttpResult(null, HttpErrors.ClientProtocolException);
    }
    catch (IOException e)
    {
        result = new HttpResult(HttpErrors.IOException);
    }

    httpclient.getConnectionManager().shutdown();
    return result;
}

That’s all we need on the client (Android) side, those methods cover all your needs of sending large files to the server.

ASP implementation

Handlefile method

On the server side, you need to implement this method as you are accessing the page.

This method will read the input stream bytes in the client request, and saves the file by calling the Save() method.

Additional data may be set as parameters in the URL query string. Anyway, this is just a suggestion.

C#
private void Handlefile()
{
    int length = (int)Request.InputStream.Length;
    byte[] buffer = new byte[length];
    Request.InputStream.Read(buffer, 0, length);
    
    Save(buffer);
}

Example for a Save method:

C#
void Save(byte[] bytes)
{
    System.IO.File.WriteAllBytes("C:\MyServerData\new_file.bin", bytes);
}

ASP Web.config file

What is Web.config?

Web.config file is a configuration file in xml format. It specifies configurations for your ASP application.

Those configurations can be set in this file or set programmatically.

One important advantage of this file is that you can change your program settings without touching your compiled code on the server.

For example, when debugging your application, usually you will want to get errors from the server. That can be accomplished by specifying:

XML
<configuration>
  <system.web>
       <customErrors mode="On"/>
  </system.web>
</configuration>

After your code is compiled and set on the IIS you can change its mode value as you wish.

Setting our Web.config configurations

In our case, we need to specify other parameters.

We are going to specify httpRuntime node under the system.web node.

XML
</system.web>
    <httpRuntime maxRequestLength="102400" executionTimeout="3600" />
</system.web>

Here we are specifying the maximum length of the files (in our case its set to 100MB) and execution time of the request (in our case, 1 hour).

Another requirement is:

XML
<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="104857600" />
  </requestFiltering>
</security>

Which specifies the maximum value of the Http request limit (in our case is set to 100MB).

Summary

We implemented both client and server sides in this article. Once it is set on your application you will not be concerned again about file transportation from android to ASP.

My code examples were simplified intentionally, since it depends on your program, and when and where you decide to use them.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe method GetHttpParams(int) is undefined for the type HttpResult Pin
Roy Barina6-Jan-15 6:32
Roy Barina6-Jan-15 6:32 
AnswerRe: The method GetHttpParams(int) is undefined for the type HttpResult Pin
Pavel Durov7-Jan-15 19:50
professionalPavel Durov7-Jan-15 19:50 
GeneralRe: The method GetHttpParams(int) is undefined for the type HttpResult Pin
Roy Barina16-Dec-15 14:29
Roy Barina16-Dec-15 14:29 
GeneralMy Vote Of 5 Pin
reza kia28-Oct-14 17:17
professionalreza kia28-Oct-14 17:17 
QuestionAny code? Pin
Member 271004217-Oct-14 12:24
professionalMember 271004217-Oct-14 12:24 
AnswerRe: Any code? Pin
Pavel Durov18-Oct-14 5:52
professionalPavel Durov18-Oct-14 5:52 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun17-Oct-14 2:42
Humayun Kabir Mamun17-Oct-14 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.