Tuesday 29 March 2011

How to Upload File in WinCE

Introduction

This Application is designed for WinCE. It includes Web, Desktop both side applications. Its client WinCE Application Code Function Upload(string Path, String FileName) takes File Path and File Name as Input and Post it to webpage, and on the other hand website application on its page FileUpload.ashx read posted file and Save it to Defined Folder which is Later Available to download.
Here is a code to upload file:

Client Side:

#region Upload
public bool Upload(string FilePath, string FileName)
{
string Url = "HTTP://test.mtsonweb.com/fileupload.ashx"; //  Change it to your page name
string BytesConfirmedReceived = "";
int BytesSent = 0;
bool Ret = false;
ASCIIEncoding encoding = new ASCIIEncoding();
try
{

if (File.Exists(FilePath +"\\"+ FileName) == false) { return true; }

//FileInfo oInfo = new FileInfo(FilePath + "\\" + FileName);
//BytesSent = Convert.ToInt32(oInfo.Length.ToString());

Url += "?myfile=" + FileName.Trim();

FileStream fr = new FileStream(FilePath + "\\" + FileName, FileMode.Open);

BinaryReader r = new BinaryReader(fr);
byte[] FileContents = r.ReadBytes((int)fr.Length);
BytesSent = FileContents.Length;

r.Close();
fr.Close();

WebRequest oRequest = WebRequest.Create(Url);

oRequest.Method = "POST";

oRequest.Timeout = 15000;

oRequest.ContentLength = FileContents.Length;

Stream oStream = oRequest.GetRequestStream();

BinaryWriter oWriter = new BinaryWriter(oStream);

oWriter.Write(FileContents);

oWriter.Close();

oStream.Close();

WebResponse oResponse = oRequest.GetResponse();
BytesConfirmedReceived = new StreamReader(oResponse.GetResponseStream(),
Encoding.Default).ReadToEnd();

oResponse.Close();

if (BytesSent.ToString() == BytesConfirmedReceived.Trim())
{
Ret = true;
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return Ret;

}
#endregion

Website Page:

<%@ WebHandler Language="C#" Class="FileUpload" %>
using System;
using System.Xml;
using System.Data;
using System.Web;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

public class FileUpload : IHttpHandler
{
public void ProcessRequest(HttpContext oContext)
{

int BytesSent = 0;
//string LocalPath = @"C:\Inetpub\wwwroot\";

string MyFile = "";

MyFile);

try
{

MyFile = oContext.Request["myfile"].ToString().Trim();
MyFile = HttpContext.Current.Server.MapPath("~/Demo/Files/" +

ASCIIEncoding encoding = new ASCIIEncoding();

BytesSent = oContext.Request.TotalBytes;

Class1 obj = Class1.GetInstance();

obj.FileName = MyFile;
obj.FileLength = BytesSent;

byte[] InComingBinaryArray =
oContext.Request.BinaryRead(oContext.Request.TotalBytes);
obj.Data = InComingBinaryArray;

if (File.Exists(MyFile) == true)
{
File.Delete(MyFile);
}

FileStream fs = new FileStream(MyFile, FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fs);
w.Write(InComingBinaryArray);
w.Close();
fs.Close();

FileInfo oInfo = new FileInfo(MyFile);
long a = (long)BytesSent;

oContext.Response.Write(oInfo.Length.ToString());

}
catch (Exception err) { oContext.Response.Write(err.Message); }

}

public bool IsReusable { get { return true; } }

}

No comments:

Post a Comment