File upload Pocket PC -> Web page, code examples

Discussion in 'ASP .Net' started by Dan Hagerman, Dec 23, 2008.

  1. Dan Hagerman

    Dan Hagerman Guest

    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
    ----------------------------------------------------------
     
    Dan Hagerman, Dec 23, 2008
    #1
    1. Advertising

  2. Dan Hagerman

    Tamer Öz Guest

    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");
    }
    }
    "Dan Hagerman" <> wrote in message news:D...
    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
    ----------------------------------------------------------
     
    Tamer Öz, Dec 25, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,542
    Bruce Barker
    Aug 3, 2005
  2. Replies:
    5
    Views:
    405
  3. AbuTalhah

    Upload files from Pocket PC

    AbuTalhah, Aug 2, 2004, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    125
    AbuTalhah
    Aug 2, 2004
  4. Dan Hagerman
    Replies:
    2
    Views:
    132
    Dan Hagerman
    Dec 23, 2008
  5. dsnyder
    Replies:
    2
    Views:
    564
    David Snyder
    Apr 14, 2004
Loading...

Share This Page