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 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
----------------------------------------------------------
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 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
----------------------------------------------------------