File upload Pocket PC -> Web page, code examples

D

Dan Hagerman

I've been struggling with uploading a file from a Pocket PC (.Net Compact
Framework v2) app to a web page (ASP.Net 2.0). I can successfully upload to
my page from an HTML page, but not from the app. I always get the "No File
received by web server" message (see code). I'd rather fix the Pocket PC
code vs. the web page code, since I know that works for uploads via HTML, but
if I need to change that end of things, so be it. I appreciate any tips!
Thanks.

file.aspx.vb code:
----------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Request.Files.Count = 0 Then
Response.Write("No file received by web server.")
Exit Sub
End If

Dim filepath As String = Server.MapPath("~/images")

Dim userPostedFile As HttpPostedFile = Request.Files(0)

userPostedFile.SaveAs(filepath & "\" &
System.IO.Path.GetFileName(userPostedFile.FileName))

Response.Write("Success" & vbCrLf)

End Sub
----------------------------------------------------------


Pocket PC code:
----------------------------------------------------------
Private Function SendFile(ByVal ImageFile As String) As Boolean

'send to web site
Dim UploadURL As String = tURL & "file.aspx"

Dim WebReq As HttpWebRequest =
CType(WebRequest.Create(UploadURL), HttpWebRequest)
WebReq.Method = "POST"
WebReq.AllowWriteStreamBuffering = True

'retrieve request stream
Dim reqStream As Stream = WebReq.GetRequestStream()

'open the local file
Dim rdr As FileStream = New FileStream(ImageFile, FileMode.Open)

'allocate byte buffer to hold file contents
Dim inData(4096) As Byte

'loop through the local file reading each data block
'and writing to the request stream buffer
Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)

While bytesRead > 0
reqStream.Write(inData, 0, bytesRead)
bytesRead = rdr.Read(inData, 0, inData.Length)
End While

rdr.Close()
rdr = Nothing
reqStream.Close()
reqStream = Nothing

Dim WebResp As HttpWebResponse = CType(WebReq.GetResponse(),
HttpWebResponse)

'process response

End Function
----------------------------------------------------------
 
T

Tamer Öz

Try using web services.

Here is an example.

Mobile App

Stream objStream;
byte[] parFile;
objStream = File.Open(wtbFilePath.Value.ToString().ToLower(), FileMode.Open, FileAccess.Read);
parFile = new byte[objStream.Length];
objStream.Read(parFile, 0, ((int)objStream.Length));
objStream.Close();

BTLMobileLayers.File.Files insFiles = new BTLMobileLayers.File.Files();
try
{

insFiles.FileUpload(fileNameHere, parFile);
}
catch (System.Web.Services.Protocols.SoapException ex)
{

}

Web Service

public void FileUpload(string fileName, byte[] objFile)
{


string root = System.Configuration.ConfigurationManager.AppSettings["root"];

if (!File.Exists(root + fileName))
{
Stream objFileStream = File.Open(root + fileName, FileMode.Create, FileAccess.Write);
objFileStream.Write(objFile, 0, objFile.Length);
objFileStream.Close();

}
else
{
throw new Exception("File Exists");
}
}
I've been struggling with uploading a file from a Pocket PC (.Net Compact
Framework v2) app to a web page (ASP.Net 2.0). I can successfully upload to
my page from an HTML page, but not from the app. I always get the "No File
received by web server" message (see code). I'd rather fix the Pocket PC
code vs. the web page code, since I know that works for uploads via HTML, but
if I need to change that end of things, so be it. I appreciate any tips!
Thanks.

file.aspx.vb code:
----------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Request.Files.Count = 0 Then
Response.Write("No file received by web server.")
Exit Sub
End If

Dim filepath As String = Server.MapPath("~/images")

Dim userPostedFile As HttpPostedFile = Request.Files(0)

userPostedFile.SaveAs(filepath & "\" &
System.IO.Path.GetFileName(userPostedFile.FileName))

Response.Write("Success" & vbCrLf)

End Sub
----------------------------------------------------------


Pocket PC code:
----------------------------------------------------------
Private Function SendFile(ByVal ImageFile As String) As Boolean

'send to web site
Dim UploadURL As String = tURL & "file.aspx"

Dim WebReq As HttpWebRequest =
CType(WebRequest.Create(UploadURL), HttpWebRequest)
WebReq.Method = "POST"
WebReq.AllowWriteStreamBuffering = True

'retrieve request stream
Dim reqStream As Stream = WebReq.GetRequestStream()

'open the local file
Dim rdr As FileStream = New FileStream(ImageFile, FileMode.Open)

'allocate byte buffer to hold file contents
Dim inData(4096) As Byte

'loop through the local file reading each data block
'and writing to the request stream buffer
Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)

While bytesRead > 0
reqStream.Write(inData, 0, bytesRead)
bytesRead = rdr.Read(inData, 0, inData.Length)
End While

rdr.Close()
rdr = Nothing
reqStream.Close()
reqStream = Nothing

Dim WebResp As HttpWebResponse = CType(WebReq.GetResponse(),
HttpWebResponse)

'process response

End Function
----------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top