Hi, I found myself a difficulty on creating a image uploader to my database. This is because in .NET CF, I cant find File Upload control. Is there any other options and how should I do it?

I know there is a OpenFileControl but there is a problem on saving it / previewing it on a picture box.

Thanks for any help.

Dani AI

Generated

As suggested, keep the upload logic on the server (a web service/handler) and have the PDA post raw bytes to it rather than trying to PUT files straight to an IIS folder. A simple, reliable pattern is: client reads the image into a byte[] and calls an ASMX/WSDL or basic-HTTP endpoint that saves the bytes with File.WriteAllBytes. A plain HTTP PUT to a directory commonly fails with 405 (server won’t accept the verb unless WebDAV or a custom handler is enabled), and DirectoryNotFoundException usually means the server-side physical path doesn’t exist. (aspsnippets.com)

Server-side (VB, ASMX) — make the webmethod create the folder if missing and write the file:

<WebMethod()>
Public Function UploadImage(fileName As String, data As Byte()) As Boolean
    Dim imagesDir As String = Server.MapPath("~/Images")
    If Not Directory.Exists(imagesDir) Then Directory.CreateDirectory(imagesDir)
    Dim target As String = Path.Combine(imagesDir, Path.GetFileName(fileName))
    File.WriteAllBytes(target, data)
    Return True
End Function

Use Server.MapPath so you’re writing to a real filesystem path and ensure the folder exists. (learn.microsoft.com)

Client-side (Compact Framework VB) — read the file and call the web service:

Dim bytes(CInt(fs.Length)-1) As Byte
Using fs As New FileStream(localPath, FileMode.Open, FileAccess.Read)
    fs.Read(bytes, 0, bytes.Length)
End Using
svc.UploadImage(Path.GetFileName(localPath), bytes)

If images are large, send in chunks or use a streaming-friendly API; CF/ASMX will work for typical device photos. (aspsnippets.com)

Troubleshooting checklist:

  • Verify the final upload URL includes the separating slash ("/Images/filename.jpg") — bad concatenation causes wrong requests.
  • If you see 405, don’t keep using PUT unless the server is configured for it (IIS + WebDAV/handler); prefer POST/ASMX/ASHX. (learn.microsoft.com)
  • If DirectoryNotFoundException appears, log Server.MapPath output and confirm the directory exists and is writable. Give the IIS app pool identity (IIS AppPool\<name> or IIS_IUSRS) Modify permission on that folder. (gnu.org)

Following that flow (client: bytes → server webmethod → File.WriteAllBytes) is the simplest, most portable solution for .NET Compact Framework devices.

Recommended Answers

All 4 Replies

>Is there any other options and how should I do it?

You need to use WebService.

>Is there any other options and how should I do it?

You need to use WebService.

Hi, yes I am using Web Service, Access 2007 accdb as my database. (The retrieve records such as Login from other components works fine)

So, do you mean the upload code are suppose to be on WebService, and the openfile dialog will make use of the web service's upload class?

However, if I use Webservice, I will get an error of "System.IO.DirectoryNotFoundException"

If I chose to implement this upload function right below all subs (Within the PDA's class), I will get a 405 error.

The code is as follows:

Public Sub UploadFileBinary(ByVal localFile As String, ByVal uploadUrl As String)
        Try
            Dim req As HttpWebRequest = DirectCast(WebRequest.Create(uploadUrl), HttpWebRequest)

            req.Method = "PUT"
            req.AllowWriteStreamBuffering = True

            ' Retrieve request stream 
            Dim reqStream As Stream = req.GetRequestStream()

            ' Open the local file
            Dim rdr As New FileStream(localFile, FileMode.Open)

            ' Allocate byte buffer to hold file contents
            Dim inData As Byte() = New Byte(4095) {}

            ' 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()
            reqStream.Close()

            req.GetResponse()
        Catch ex As Exception
            MsgBox("Unable to connect error:" & Chr(10) & ex.Message)
        End Try
    End Sub

To call the above subroutine, I used the following code on Button click event (Submit)

'Upload the image
                Dim UploadURL As String = "" + Path.GetFileName(txtImg.Text)
                UploadFileBinary(txtImg.Text, UploadURL)
'Where UploadURL is a string contains my computer name and the actual directory and txtImg.Text is the read-only textbox that shows the selected image to be uploaded.

PS: If I typed "" without quotes in the browser, I can see the directory listing. Neither is there a prompt of administrator rights when I tried to copy a file to that directory.

I also did

Imports System.IO

on the first line as well.

Update:

I forgot to mention that I had used

Imports System.Net

as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.