summaryrefslogtreecommitdiff
path: root/src/com/lc8n/util/FileUploader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/lc8n/util/FileUploader.java')
-rw-r--r--src/com/lc8n/util/FileUploader.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/com/lc8n/util/FileUploader.java b/src/com/lc8n/util/FileUploader.java
new file mode 100644
index 0000000..815c33a
--- /dev/null
+++ b/src/com/lc8n/util/FileUploader.java
@@ -0,0 +1,106 @@
+/**
+ *
+ */
+package com.lc8n.util;
+
+import java.io.File;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.util.Log;
+
+import com.lc8n.util.ProgressMultipartEntity;
+import com.lc8n.util.ProgressMultipartEntity.ProgressListener;
+
+
+/**
+ * @author joe
+ *
+ */
+public class FileUploader extends AsyncTask<File, Integer, String> {
+
+ ProgressDialog pd;
+ int totalSize;
+ protected Context mContext;
+
+ public FileUploader(Context context) {
+
+ mContext = context;
+ }
+ @Override
+ protected void onPreExecute()
+ {
+
+ }
+
+ public void prepare(int max) {
+ pd = new ProgressDialog(mContext);
+ pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+ pd.setMessage("Uploading File...");
+ pd.setCancelable(false);
+ pd.setMax(max);
+ pd.show();
+ }
+
+ @Override
+ protected String doInBackground(File... file) {
+
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpContext httpContext = new BasicHttpContext();
+ HttpPost httpPost = new HttpPost("http://www.blaupload.co.uk/upload_file.php");
+ Log.d("File", file[0].getName());
+
+ try
+ {
+ ProgressMultipartEntity multipartContent = new ProgressMultipartEntity(new ProgressListener()
+ {
+
+ public void transferred(long num)
+ {
+ Log.d("Progress", String.valueOf(num));
+ publishProgress((int)(num/1024));
+ }
+ });
+
+ // We use FileBody to transfer an image
+ multipartContent.addPart("file", new FileBody(file[0]));
+
+ totalSize = (int)(multipartContent.getContentLength()/1024);
+ Log.d("Size", String.valueOf(totalSize));
+ // Send it
+ httpPost.setEntity(multipartContent);
+ HttpResponse response = httpClient.execute(httpPost, httpContext);
+ String serverResponse = EntityUtils.toString(response.getEntity());
+
+ return serverResponse;
+ }
+
+ catch (Exception e)
+ {
+ System.out.println(e);
+ }
+ return null;
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... progress)
+ {
+ pd.setProgress((int) (progress[0]));
+ }
+
+ @Override
+ protected void onPostExecute(String a)
+ {
+ pd.dismiss();
+ }
+}