Results 1 to 12 of 12

Thread: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    There are times when you need your program to obtain, create, or update remote files. Within a LAN this is normally no problem and you can just use a regular file share. But what if your program is being used across the Internet? Now what?

    Or maybe you're creating the latest and greatest Web site development tool. But you need a way for your program to "publish" the pages and images to the user's Web server?

    Sometimes the answer might be FTP. This seems to be the default action of most VB programmers anyway. Yet FTP has a number of limitations, including a firewall-hostile protocol that exchanges logons and data in plain text.


    Is there another answer?

    The majority of Web servers support (or can support) two common "Read/Write Web" protocols that work as extensions to common HTTP (or HTTPS for security).

    The older of these is known as FrontPage Server Extensions (FPSE) and is actually very common. The other is a little newer and in some ways more versatile. It is called Web Distributed Authoring and Versioning (WebDAV, or DAV) and is not available with all cheap Web hosting plans. Both IIS (even through IIS 7) and Apache can work with either FPSE or WebDAV though, once you install the options and enable them.

    While hardly the same as a local file share, you get a lot of capability from FPSE/DAV without a lot of trouble. The limitation is that you must work with the files as data streams, i.e. like a sequential file. No random I/O, no using it to host something like a Jet MDB database. Think "open and save."


    How does this help the VB programmer?

    While unknown to a lot of people, ADO was designed to handle a lot more than just database access.

    Beginning with MDAC 2.5 ADO gained a lot of capabilities. Included in these is the ability to work with the new Internet Publishing Provider (MSDAIPP.DSO) as well as various XML and other Data Source Objects (DSOs).

    This means you can use ADO to read, write, download, and upload data from and to your Web site. Anywhere on the Internet.


    WebSpring

    To make this a bit easier to use I created a wrapper class that I call WebSpring.

    A small joke there: you work with the Web files via ADODB.Stream objects, and streams flow from a spring... get it? Ok, a very small joke.

    WebSpring comes with two auxiliary classes. One of these is WSDirItem which contains a file's attributes from a directory request. The other is WSDirItems which is the collection of WSDirItem objects that the directory request returns to you. There is a third one of course: the ADODB.Stream class.

    WebSpring offers several Methods:
    • Connect()
    • Delete()
    • DirItems()
    • Disconnect()
    • DownloadFile()
    • Exists()
    • MakeDir()
    • OpenFile()
    • UploadFile()

    There is much more detail on these methods and on WebSpring's Properties in comments at the head of the code.

    WebSpring does not attempt to expose all of the functionality available. However it does provide quite a lot, and you have the source code if you want to extend it.

    When you look at my code you'll find most of it shockingly simple. Go ahead, make my day: code directly against ADO yourself. And what has been stopping you all these (nine) years then?


    So what does WebSpring buy me?

    WebSpring gives you an easy to use way to add Web site publishing to a VB6 program.

    More than this, it allows you to use a FPSE Web site or a WebDAV host as a read/write central data respository. Think of it as a basic version of the "Cloud Computing" we're beginning to read so much about.

    If you have a WebDAV site, you might set up a client account and embed its credentials (User/PW) in your program. Then you can give that User read-only access to one folder and write-only access to another. This could be used to check for and download new program versions for autoupdating or new data files. It could be used for submitting registration info, crash reports, feedback form data, etc.

    Pretty much anything you've been resorting to FTP for, except that it is:
    • Easy to use,
    • Firewall friendly,
    • Secure,
    • Can read to and write from memory as well as disk!


    What special knowledge should I have to apply WebSpring?

    It would be very helpful to read up on the ADODB.Stream object. When you use the WebSpring.OpenFile() method the results are returned to you as an open Stream object.

    As you study ADODB.Stream you'll find the ways you can use it to process binary data or text data. It can even handle conversions to or from Unicode, ASCII, UTF-8, etc. as well as alternate line delimiters (LF, CR, or CRLF).

    Just like opening a text file sequentially with VB's Open statement, you can't just "insert and delete" data. But in much the same way you can append or replace data. Text can be read or written by line or by a number of characters. Binary data can be read in chunks or all at once. You can move the "cursor" much like using a VB Seek statement.


    WSDemo

    The attached archive contains the WebSpring classes and a sample project that will excercise most of WebSpring's capabilities.

    To use it you will need a Web site that is either FrontPage or WebDAV enabled. Surprisingly many already do support one or the other. In lieu of this you could run IIS or Apache with the proper extensions installed and enabled.

    Then you'll need the URL of the site and a User/PW that has full access to your site. For many simple Web sites this is the same User/PW account you use with FTP to update the site.


    Beyond file I/O

    Another thing that can be done with a FPSE-enabled site is to pass parameter values to WebSpring.OpenFile() that cause it to do an HTTP GET request. This can be used with a scripted Web page to actually pass GET parameters to and run the script, returning the script output in the Stream you get back.

    This is documented to work for ASP pages with IIS. I can think of no reason why it shouldn't work as well for an Apache/PHP page.

    This would give you even more "Cloud" functionality. Not just file I/O but also the ability to inquire/update a database at the server!


    Cautions

    Many of the free Web hosting providers do not like you to make exclusive use of a free Web site for this purpose. Some want to add banner ads and such to your Web pages. others have no banners but their Terms of Service prohibit use as a data repository.

    You might have to:
    • Self-host on a server of your own (simple enough to do, and only a simple firewall rule or port mapping requierd),
    • Use the webspace provided in your ISP account (unless it also prohibits data-only use), or
    • Find a pay provider offering either WebDAV hosting or more permissive FPSE-enabled Web hosting.
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program


  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    Sadly an Office Web Folders update can break this functionality if installed in Vista, and it seems to be part of Win7 so that's broken as well.

    However there are other approaches you can build on top of WinHttpRequest or XmlHttpRequest objects. They just require you to implement a large enough subset of WebDAV yourself to do what your programs need.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    I've decided to post an update to this thread that offers an example of the sort of alternative suggested in the last post. Enough people have come along to read this thread that I hate to leave everyone hanging.


    MiniDAVShare

    This is a small VB6 class built on top of the MSXML.XmlHttpRequest object.

    While it is not full-featured, omitting many WebDAV operations including locking and other nuances, it does enough in a compact form tobe useful for many applications. Most of the features that are implemented should be supported by most WebDAV servers though.

    This class gives you access to a remote WebDAV server account where you can fetch a directory listing with several attributes, get and put a file as binary data or as ANSI or Unicode (UTF-16LE) text, make directories, delete directories and files, copy and move directories and files, etc.


    A Demonstration

    The attachment contains MiniDAVShare.cls and two small projects that make use of it: DataManager and DataConsumer.

    DataManager can be used to view and edit a simple list of data, and to persist the changed list to disk and post it to the WebDAV server.

    DataConsumer can retrieve this list from the server and display it. Retrieved data is persisted to disk along with a "last updated" timestamp. This timestamp is used to query the server for an updated list, which is then retrieved and stored for use locally.


    Data Formats

    The list of data is stored as a persisted ADO Recordset in XML format. The timestamp is stored locally at the DataConsumer machines in VB6's Write # format in a separate local file.

    Your programs could use any other formats you wish, text or binary. You might post arbitrary XML, CSV, text, a Jet MDB, or whatever else your programs require.


    Helper Classes

    Both projects isolate some of the fiddly ADO-related and local persistence logic out into xxxHelper classes. This helps keep the logic in the Forms a little cleaner and easier to follow. All of that could easily be inline code within the Forms though.


    "Connection Strings"

    Both projects use a hard-coded String value containing their "connection parameters." Real programs might do this as well, or they might fetch these values from a config file or something. You'd probably want to do some encryption of at least the password value though, especially in a program dstributed publicly.

    Dummy example in the attachment:

    Code:
    'CONN syntax: BaseURL|SubDir|User|PW
    Private Const CONN As String = _
        "https://webdav.mydrive.ch|DataDemo|owneracct|somepassword"

    Running the Demo

    To run the demo you will have to have an account with read/write/create access on a WebDAV server.

    You can set most versions of IIS up with WebDAV for testing, and many Apache versions have a module for WebDAV support as well. But for this to be of much use you'll want a publicly visible WebDAV server and for most of us it isn't practical to expose our own servers to the Internet and keep them up 24 by 7.

    I've had good luck with MyDrive.

    They offer a free account with 2GB of storage, and better yet they have a 2-tired account scheme where you have an owner account and sub-accounts. You get 1 sub-account for free.

    For this demo the subdirectory that the data gets posted to is created by the DataManager program. Since MiniDAVShare doesn't have any way to set permissions, the DataConsumer demo would also need to use the owner account.

    For a real application similar to the demo you'd probably want to use their Web interface to create the subdirectory and give your guest/limited-user subaccount read only access. Then the DataConsumer piece of your application only needs that subaccount and password rather than your owner credentials.

    For applications where the client programs must post data you could of course grant the limited subaccount read/write access to a given directory.

    I'm not sure which other providers offer this sort of owner vs. limited-user access. It's a great feature though! I really can't imagine why this isn't more widely available.

    So once you have your server and account, modify the CONN string value in the two projects to test them. Some WebDAV servers require a URL of the form:


    http://someguy.com/~account


    Where account is your account user ID.

    MyDrive figures out where your data lives there based on the user ID passed to it as security credentials.


    Way Better than File Sync Services

    Unlike numerous "cloud storage" offerings, including some clunky single-user WebDAV services, this approach makes WebDAV a useful part of application design. There are other Web Service offerings based on numerous ad-hoc (and often extremely clumsy to implement) protocols you could use, but WebDAV client capability is very light weight to implement as illustrated by MiniDAVShare.

    With some effort you could probably configure IIS or Apache with WebDAV to work in a similar fashion as well of course.
    Attached Images Attached Images  
    Attached Files Attached Files

  5. #5
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    Interesting. Actually, I don't understand well about your program.

    I run your demo "Data Manager". It created a folder (DataDemo) and put a file data.xml in folder \DataDemo.

    Name:  webspring_1.JPG
Views: 1669
Size:  20.3 KB


    1. I want to put a picture with 400K size (c:\testpic.jpg) into folder \DataDemo, how to do?
    2. If an existing file is access file e.g. mydatabase.mdb in folder \DataDemo, can I create a VB program to retrieve/modify the tables and fields using ADO?

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    I haven't tried a large picture, but this demo works fine with a smaller one:

    Code:
    Option Explicit
    
    Private DAV As MiniDAVShare
    
    Private Sub cmdFetch_Click()
        Dim F As Integer
        Dim Blob() As Byte
        
        With DAV
            .GetFileBytes "pic.jpg", Blob
            lblStatus.Caption = CStr(.Status) & " " & .StatusText
            If .IsStatus2XX Then
                On Error Resume Next
                Kill "fetched.jpg"
                On Error GoTo 0
                F = FreeFile(0)
                Open "fetched.jpg" For Binary Access Write As #F
                Put #F, , Blob
                Close #F
                imgFetched.Picture = LoadPicture("fetched.jpg")
            End If
        End With
    End Sub
    
    Private Sub cmdPost_Click()
        Dim F As Integer
        Dim Blob() As Byte
        
        With dlgOpen
            .DialogTitle = "Select an image to post"
            .CancelError = True
            .Filter = "JPEG images (*.jpg)|*.jpg"
            .Flags = cdlOFNExplorer _
                  Or cdlOFNFileMustExist _
                  Or cdlOFNLongNames _
                  Or cdlOFNPathMustExist _
                  Or cdlOFNShareAware
            On Error Resume Next
            .ShowOpen
            If Err Then Exit Sub 'Canceled.
            On Error GoTo 0
            imgPost.Picture = LoadPicture(.FileName)
            F = FreeFile(0)
            Open .FileName For Binary Access Read As #F
            ReDim Blob(LOF(F) - 1)
            Get #F, , Blob
            Close #F
        End With
        With DAV
            .PutFileBytes "pic.jpg", Blob
            lblStatus.Caption = CStr(.Status) & " " & .StatusText
        End With
    End Sub
    
    Private Sub Form_Load()
        Set DAV = New MiniDAVShare
        With DAV
            .BaseURL = "https://webdav.mydrive.ch/DataDemo"
            .User = "myuser"
            .PW = "mypassword"
        End With
    End Sub
    Ok, retested using a 1.1MB picture and it works fine though it takes a while. For larger files you'd probably want to rework the MiniDAVShare class to use the WintHttpRequest object instead since it can be used asynchronously.

    If you can't handle that yourself I suggest you hire a programmer though. I'm not bored enough to do it right now.


    If you upload an MDB file there is no way to use it directly via ADO. You'd have to download a copy to a local drive and use it there.

    Think of this as more of a way to exchange small files or to post and retrieve data updates for your programs. If you want a remote shared database I suggest you look at the Web Services designed to support that such as Amazon SimpleDB and many others.
    Attached Files Attached Files
    Last edited by dilettante; Dec 6th, 2012 at 10:00 PM. Reason: Updated to use ImageBox with Stretch instead of PictureBox controls

  7. #7
    Frenzied Member
    Join Date
    Jan 2010
    Posts
    1,103

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    If the class has callback function, then we know the percentage of sending/receiving.

    By the way, If the folder has space,we should put "/".
    .BaseURL = "https://webdav.mydrive.ch/My Pictures/"

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    Odd, since the BaseURL property adds a "/" at the end if you don't provide one.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    Quote Originally Posted by Jonney View Post
    If the class has callback function, then we know the percentage of sending/receiving.
    I don't think the MSXML.XMLHTTP ("XmlHttpRequest") offers any progress callbacks or events that are easy to use in a VB6 program. You can do it, but it requires an additional "event sink" class because of the script-oriented callbacks it does.

    The WinHttpRequest object offers events supporting async operation without a lot of kludging in VB6. However it does not provide "progress" as such.

    So yes with a little work you can execute the HTTP methods asynchronously using either object. There isn't really a "progress feedback" facility though.


    The point of MiniDAVShare was, well, to be a "mini" example. A starting point, a code sample, showing a working example of what can be done using VB6.

    You can always buy a full-featured WebDAV client library too.

  10. #10
    Member
    Join Date
    May 2013
    Posts
    47

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    why upload gif is o kb
    Name:  1.jpg
Views: 3051
Size:  29.6 KB
    Name:  2.jpg
Views: 1155
Size:  30.4 KB
    Name:  3.jpg
Views: 1017
Size:  2.5 KB

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    I'm not sure.

    However Microsoft has made changes to ADO and "web folders" that basically broke "WebSpring" so we have to use alternatives now. See post #4 above.

  12. #12
    Member
    Join Date
    May 2013
    Posts
    47

    Re: VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program

    OK,thanks 。

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width