# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Winsock: HTTP File Upload

## visualAd

*Introduction*
One of the most useful extensions to the HTTP protocol, is the ability to use it to upload files. Although, compared to FTP it is somewhat limited, all that is required to make use of HTTP file uploads is access to a web browser.

Transferring files using HTTP is generally safer than FTP as the data sent to the server can be carefully scrutinised before the commitment is made to store / use it. For this reason it is ideal in client/server applications which regularly need to transfer data over the Internet.

The following application, demonstrates how to upload files to a web server using Visual Basic, with the help of the winsock control. In order to add the Winsock Control to your VB6 application, right click on the tool box, select components from the menu and check the box "Microsoft Winsock Control".

This mimics what a web browser does when you upload a file to a site via a web form.

*A Bit about HTTP*

The Winsock control enables you to establish a connection to another computer using either TCP / UDP and transfer data over that connection. HTTP (Hypertext Transfer Protocol) is used to request and send data to web resources and it is up to the programmer to construct the HTTP request and parse the response.

The file upload application uses a HTTP POST request to send the file in the form of a multi part HTTP request. This is explained fully in RFC 1867

*Using the Demo Application*

The demo application allows you to enter a URL and choose the file which you wish to upload. You must give the file a name and an MIME type; a list of MIME types for particular file extensions can be found here.

Pressing the upload button sends the HTTP request containing the data from the file which you have chosen.

Like I mentioned earlier HTTP file uploads are useful in client / server applications. So, I have made a script which will take file uploads to demonstrate how the application works, it also has a web interface:

http://adam.codedv.com/examples/post_dump.php

The files which have been uploaded can be found here, it is clear that there is no conceivable difference between files uploaded via VB and via a web browser. The means in which the server gets the data is transparent.

----------


## sabricka

plz send to me the (post_dump.php) file to put it in my site and try it 
send it to 
hanysabra@hotmail.com

----------


## Hack

> plz send to me the (post_dump.php) file to put it in my site and try it 
> send it to


First, welcome to the forums.

Second, you should never, ever put your email address in any open forum post anywhere.   It is very easy for spam bots to pick that up.

If you wish to let one of our members know what your email address, please use our internal private messenging system.     Thanks.

----------


## visualAd

Everyone seems to want the source code for the PHP files. So, here they are.

The post_dump.php file simply loops through all the posted variables and files and displays information on them. It saves the files in another directory, so to use it you need to create two directories:

*uploaded_files*
*uploaded_files/files*

*post_dump.php*


```

<?php
    /* check for and loop through uploaded files */
    foreach ($_FILES as $name => $info) {
        /* chekc the file has a valid name */
        if (($info['name'] != 'ht.access' &&
            substr($info['name'], 0,1) != '.' &&
            $info['name'] != 'allowed_files')) {

            /* attempt to move the file to the uploaded_files directory */
            if (@move_uploaded_file($info['tmp_name'], "uploaded_files/files/{$info['name']}")) {
                /* get the contenst of the array of files */
                $file_arrays = @unserialize(@file_get_contents('uploaded_files/allowed_files'));        
    
                /* if there are more already 5 files, remove the oldest item fomr the array
                 * and delete it 
                 */
                if (count(@$file_arrays['files']) == 5) {
                    $name = array_shift($file_arrays['files']);
            
                    unset($file_arrays['types'][$name]);
                    @unlink('uploaded_files/files/' . $name);
                }

                /* add the new item to the arrays */
                $file_arrays['files'][] = $info['name'];
                $file_arrays['types'][$info['name']] = $info['type'];

                /* serialize the array and write it back to the file */
                if ($fhwnd = @fopen('uploaded_files/allowed_files', 'wb+')) {
                    fwrite($fhwnd, serialize($file_arrays));
                    fclose($fhwnd);
                }
            }          
        }   
    }
?>
<html>
    <head>
        <title>Form Post Dump</title>
    </head>
    <body>
        <?php if ($_POST): ?>
            <p>You submitted the following POST variables with the following names:</p>
            <ul><?php foreach($_POST as $name => $var) : ?>
                <li><?php echo(htmlspecialchars($name)) ?> = <?php echo(htmlspecialchars($var)) ?></li>
            <?php endforeach; ?></ul>
        <?php endif; ?>
        <?php if ($_GET): ?>
            <p>You submitted the following fariables with the query string:</p>
            <ul><?php foreach($_GET as $name => $var) : ?>
                <li><?php echo(htmlspecialchars($name)) ?> = <?php echo(htmlspecialchars($var)) ?></li>
            <?php endforeach; ?></ul>
        <?php endif; ?>
        <?php if ($_FILES): ?>
            <p>You submitted the following FILES:</p>
            <ul><?php foreach($_FILES as $name => $info): ?>
                <li>File name: <?php echo(htmlspecialchars($name)) ?>
            <ul>
                <li>Client file name: <?php echo(htmlspecialchars($info['name'])) ?></li>
                <li>File MIME type: <?php echo(htmlspecialchars($info['type'])) ?></li>
                <li>File size: <?php echo(htmlspecialchars($info['size'])) ?></li>
                <li>Download link: <a href="uploaded_files.php?file=<?php echo(htmlspecialchars($info['name'])) ?>"><?php echo(htmlspecialchars($info['name'])) ?></a></li>
            </ul>
            </li>
        </ul>
            <?php endforeach; ?></ul>
        <?php endif; ?>
        <form enctype="multipart/form-data" action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post">
            <p>Variable1: <input type="text" name="variable1" /></p>
            <p>Variable2: <input type="text" name="variable2" /></p>
            <p>Variable3: <input type="text" name="variable3" /></p>
            <p>File: <input type="file" name="file1" /></p>
            <p><input type="submit" value="Post" /></p>
        </form>
    </body>
</html>

```

Direct link to the source code: http://adam.codedv.com/examples/post_dump.phps

The uploaded_files.php file, displays or allows the user to download last 5 uploaded files.

*uploaded_files.php*


```

<?php
    /* load the file containg the list of uploaded files in to an array */
    $file_arrays = @unserialize(@file_get_contents('uploaded_files/allowed_files'));

    /* check the data was loaded successfully - if not, create an empty array */
    if (!is_array($file_arrays)) {
        $file_arrays = array('files' => array(), 'types' => array());
    }

    /* check for the existance of a file variable in the queery string
     * if its there, this contains the name of the file to be downlaoded
     */
    if (isset($_GET['file'])) {
        $file = $_GET['file'];
        
        /* check the file is in the array retrieved from the file */
        if (in_array($file, @$file_arrays['files'])) {
            /* get the Content-Type of the file */
            header('Content-Type: ' . $file_arrays['types'][$file]);
            header('Content-Disposition: attachment; filename="' . $file . '"');

            /* send the file */
            @readfile("uploaded_files/files/$file");
            exit;
        }
    }
?>
<html>
    <head>
        <title>Uploaded Files</title>
    </head>
    <body>
        <h3>Recently Uploaded Files</h3>
        <ul>
            <?php foreach($file_arrays['files'] as $file): ?>
                <li><a href="<?php echo($_SERVER['PHP_SELF'] . '?file=' . htmlspecialchars($file))?>">
                    <?php echo(htmlspecialchars($file)) ?></a>
                </li>
            <?php endforeach; ?>
        </ul>
    </body>
</html>

```

Direct link to the source code: http://adam.codedv.com/examples/uploaded_files.phps

----------


## sabricka

thank you too match and many thanks to this great fourm

----------


## joechez

This programs are working some how okey, but when I try to use the documents (JPG pictures or any other) online (meaning on a web page or by direct access to it from a web folder,) It just doesn't work. I can not use the files online. Why? Is there any whay to do it, or fix it. I have being trying all kinds of things: change mime type, etc. But still nothing. I will apreciate any help.

----------


## visualAd

Can you clarify what the problem is? - Is it with the server scripts or the example program? Do you get any error messages or is the file just messed up?

----------


## joechez

I just want to use the uploaded pictures on my website, but they just don't work. If I upload manually using ftp they work just fine. But if I use the script, they don't. Then using the script, I uploaded some pictures (to try out) and downloaded them using ftp. Then uploaded them againg using ftp, and they didn't show up. So, something happend with the pictures using the script. I try other documents also with the same results.

----------


## visualAd

What errors are you getting? And have you created the correct directories to place the uploaded files in?

----------


## joechez

Yes! I have created the needed folders. As I said the script is working. I can upload and download files. But the files get corrupted or something because after doing an upload, they just don't work on regular web pages. I think the attributes of the files are changing or something.

----------


## joechez

Yeep! The files work okey just changing the attributes to public read and to group read. Still looking forward to transffer variables at the same time using VB6.

----------


## harish_sv

Hi,


The above example wont work with https site.Pls let me know the solution.

Thanks
Harish

----------


## visualAd

HTTPs is HTTP tunneled through SSL. Unfortunatly this is vastly more complicated than a standard unencrypted HTTP connection and somthing which I haven't got the time or the  knowledge to do at the moment.

----------


## svhk

Hi All,

Even i am struggling with uploading files for https sites using vb 6.0.
If anybody gets the solution don't forget to post it here..Also i am looking for this solution very urgently...so pls pls post it asap.

Thanks in advanc
svhk

----------


## leigh_wardle

Hi everyone,

The PHP and VB6 code posted here works really well.

I was just wondering if it would be possible to generalize it to do what I would call "custom folders" (on the server).

I would like to be able to upload just one licence file to a few dozen different folders (one per client).  The version of the file is different for each folder.

It would nice to be able to put a loop around the VB6 upload code to make this happen.

But I don't know enough about the methodology used in the current PHP and VB6 code to do this.

Any suggestions would be most welcome.

Regards, Leigh

----------


## ionut_ro

QUESTION:

I am connected to Internet Via Proxy. How can I make this sample work?
If I have a direct connection, everything works well.

Through proxy, I am getting this error: 'Valid name, no data record of requested type' (Code 11004) Need advice..

Thank you all.

----------


## ScanCC

anyone here can upload jpg, doc, zip... by this program?  I have tried many time and can't success...
But I found that there are no problem when uploading plain text file(txt).

----------


## visualAd

It works fine for me. Do you get any errors? And are you sending the right MIME type?

----------


## ScanCC

Thank for your reply and your great program first!!

After many times of try and error, I found that it can upload jpg under ENGLISH WinXP, but not in CHINESE WinXP.

for CHINESE WinXP:
There are no error when uploading jpg file, but when I try to download the jpg file from server, I found that the content of jpg is changed and it can't be opened again.  May be it is becasue of the encoding problems?

----------


## Vladimir - SV

Hi!, in first place let me thank you for share your code *visualAd*, second a few questions about it:

1)Could your code work with "multipart/form-data" header type?
2)Wich is the max size of the file that can effectively be send?, despiting the servers limitations.
3)How necessary is to do it with _winsock.ocx_?, it's difficult to do it through winsocks APIs?, does need _winsock.ocx_ be packed along the aplication?
4)Could your code be reproducible with Wininet APIs?

Thanks in advance. Glad to see that someone is sharing this kind of hard to find but high usable code.

----------


## visualAd

> 1)Could your code work with "multipart/form-data" header type?


What do you mean? It uses the multipart/form-data header in the HTTP request and sends the HTTP request as a multipart document.




> 2)Wich is the max size of the file that can effectively be send?, despiting the servers limitations.


I am not sure probably the size of a VB string 64k. However, with anything bigger, VB should use swap or other mechanisms to handle the string. I am no VB guru so cannot say for sure.




> 3)How necessary is to do it with _winsock.ocx_?, it's difficult to do it through winsocks APIs?, does need _winsock.ocx_ be packed along the aplication?


In theory the ocx should be included. If not, you need to ensure that the control is installed on the OS you are running the program on and hope that if it is installed the version is compatible withh the one you've used.

Winsock is included by default from windows 98+, so I would imagain you'd be quite safe.




> 4)Could your code be reproducible with Wininet APIs?


I should hope so, as this is exaclty what the Winsock control is a wrapper for  :Smilie:

----------


## Vladimir - SV

> What do you mean? It uses the multipart/form-data header in the HTTP request and sends the HTTP request as a multipart document.


Yes sorry, just notice that in the code, I got confused when see this:


VB Code:
' Content-Disposition: form-data; name="UploadName"; filename="FileName"

Later I see this:

VB Code:
' Content-Type: multipart-form-data, boundary=boundary




> I am not sure probably the size of a VB string 64k. However, with anything bigger, VB should use swap or other mechanisms to handle the string. I am no VB guru so cannot say for sure.in the HTTP request and sends the HTTP request as a multipart document.


Ok, lets say I want to send a 500KBs file, supossing that limit is 64KB, I should use a string class or there's a way to send chunks of data at time?




> In theory the ocx should be included. If not, you need to ensure that the control is installed on the OS you are running the program on and hope that if it is installed the version is compatible withh the one you've used.
> Winsock is included by default from windows 98+, so I would imagain you'd be quite safe.


So it will be ok for most cases to do it with winsock.ocx rather than directly calls to winsock.dll?




> I should hope so, as this is exaclty what the Winsock control is a wrapper for


Thanks.   :Thumb:

----------


## jedelvalle

What if I need to send more data in the form. Like two other input type="text" in the same form to autenticate the user that is sending the file??? Please help.

----------


## visualAd

You need to encode each variable as a part in the HTTP request. I.e:


```
--BOUNDARY
Content-Disposition: form-data; name="var1"

value1
--BOUNDARY
Content-Disposition: form-data; name="var2"

value2
--BOUNDARY
Content-Disposition: form-data; name="file"; filename="file.txt"
Content-Type: text/plain

File text.
--BOUNDARY--
```

----------


## visualAd

I'll have a look at modifying the app to include other variables, it is easy enough to modify by yourself however. Also, have a look at the demo app for sending GET/POST HTTP requests.

----------


## hellilyntax

pls somebody..
why did i get this error msg..

HTTP/1.1 401 Access Denied
Server: Microsoft-IIS/5.1
Date: Mon, 19 Jun 2006 18:00:32 GMT
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
Content-Length: 24
Content-Type: text/html

Error: Access is Denied.

----------


## visualAd

You need to authenticate yourself using NTLM (Microsoft Windows NT LAN Manager) authentication. It is possible to do this in VB but involves a lot of byte shifting and code.

----------


## hellilyntax

thanks 4 ur reply visualAd,
pls gimme more detail, how to do so..in policy setting?
im using winxp as webserver(just for development)..

im going to host my webserver at hosting company...
what do i need to tell them to do?

ill buy u a pasta then..hehe..  :Big Grin:

----------


## visualAd

> thanks 4 ur reply visualAd,
> pls gimme more detail, how to do so..in policy setting?
> im using winxp as webserver(just for development)..
> 
> im going to host my webserver at hosting company...
> what do i need to tell them to do?
> 
> ill buy u a pasta then..hehe..


 NTLM is a challenge response protocol. That means the client needs to make a connection containing a challenge and the server makes the response. No user name and password are ever transmitted to the server - the authentication takes place with the domain controller.

Like I say. I have never written an NTLM authenticator in VB and I do not plan to either. That doesn't mean the its not possible, just that its a complex and time consuming task.

If you want to do it yourself here is a detailed specification.

http://curl.haxx.se/rfc/ntlm.html

----------


## hellilyntax

thanks a lot visualAd,
it sound quite hard for that coding...
me either didnt plan to do so...hehe...

im better choose another solution...
im planning to update my sql server directly from my vb local client instead of upload the file then update at the server.

which one is better in terms of reliability?

----------


## secra4

Hi All,
problem
look at the picture

----------


## visualAd

That is an HTTP error. It means you don't have permission to access the file.

----------


## Budro

First of all, I want to thank you for the code. I've been using it for a while and it works great. 

Now for my question. Has anyone used a progress bar with the winsock control for uploading files? I'm new to progress bars and want to add one to this code for my program users. Any suggestions, refrences or code would be greatly appreciated.

Thanks in advance

----------


## JulieWSFI

This is fantastic.  

Has anyone created an ASP version of the PHP code ?

Thank-you.

Juls

----------


## iPrank

Hey Visual,
any idea how to modify this code for imagesack.us or similar sites ? These sites redirects to another page (which contains the new links) after uploading.

I tried to upload google2.gif to imagesack usin your code and got this:

*Upload URL:* 

```
tried these two:
http://www.imageshack.us/transload.php
http://imageshack.us/ie.php
```

*HTTP Request:*

```
POST /transload.php? HTTP/1.0
Host: www.imageshack.us
Content-Type: multipart/form-data, boundary=tf63uHQ4SWyMnYsVco1QyHEi5qlB08uv
Content-Length: 1739

--tf63uHQ4SWyMnYsVco1QyHEi5qlB08uv
Content-Disposition: form-data; name="file"; filename="google2.gif"
Content-Type: application/octet-stream

GIF89a4
```

*HTTP Response:*


```
HTTP/1.0 302 Found
Connection: close
X-Powered-By: PHP/4.4.2
location: http://www.imageshack.us/
Content-type: text/html
Content-Length: 0
Date: Mon, 18 Dec 2006 19:33:19 GMT
Server: lighttpd/1.4.8
```

*1. How do I get the new links of uploade file ?
2. What changes need to be made for registered users ?* (Imagesack doesn't allow image deletion for unregistered users)

----------


## litlewiki

Try the one from vbip.com.It detects redirects.I'm not sure though if this is what you asked for:
http://www.vbip.com/winsock/winsock_http_07_01.asp

----------


## iPrank

Sorry for the confusion. I shouldn't have said 'redirection'.
I tried to mean, when you finish uploading files in imagesack (or YahooMail) they opens a new page that contains URL of the newly uploaded file. (In case of YMail, it is the 'finish attatching file and continue to message' page.)

Getting the HTML of this 'uploading-finished-and-here-is-your-URL' page is necessary. Without it I can't get URL of my own images.

When I tried to mix Visual's and VBIP's code, I got this as 'new URL'.


```
http://www.imageshack.us/2 Found
```

and there is still my second question. How to login to that site with Winsock and upload to that account. (OK, I can use Webbrowser control, but that would be slow.)

[I need this 'cause I want to make a smily collection app like the one Static was making, but want to store the images in ImageSack for easier sharing]

----------


## litlewiki

You need to have cookies enabled for that and also it returns a 302(temporarily moved) which means it is not handling the redirects(not exactly redirects but kinda) properly.

I don't how far i can help you out but i can suggest these:

*1.Get live http headers extension for Firefox.:* 
http://livehttpheaders.mozdev.org/ 

*2.Check out the way things work in the background: Here is my output:* 



```
http://reg.imageshack.us/setlogin.ph...051795891XXXXX
GET /setlogin.php?login=eef5cc5e36b051795891XXXXX HTTP/1.1
Host: reg.imageshack.us
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 302 Found
Connection: close
X-Powered-By: PHP/5.1.2
Location: http://dev.imageshack.us/setlogin.ph...xxxxxxxxx&xml=
Content-Type: text/html
Content-Length: 0
Date: Tue, 19 Dec 2006 07:13:18 GMT
Server: lighttpd/1.4.8
----------------------------------------------------------
http://dev.imageshack.us/setlogin.ph...xxxxxxxxx&xml=

GET /setlogin.php?login=eef5cc5e36b051795891xxxxxxxxx&xml= HTTP/1.1
Host: dev.imageshack.us
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 302 Found
Date: Tue, 19 Dec 2006 07:09:08 GMT
Server: Apache/2.0.52 (CentOS)
X-Powered-By: PHP/4.3.9
Set-Cookie: myid=3733953; expires=Fri, 14-Dec-2007 07:09:08 GMT; path=/; domain=.imageshack.us
Set-Cookie: myimages=eef5cc5e36b051795891xxxxxxf3; expires=Fri, 14-Dec-2007 07:09:08 GMT; path=/; domain=.imageshack.us
Location: http://reg.imageshack.us/v_images.php
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://reg.imageshack.us/v_images.php

GET /v_images.php HTTP/1.1
Host: reg.imageshack.us
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: myid=3733953; myimages=eef5cc5e36b051795891xxxxx90f3

HTTP/1.x 200 OK
Connection: close
X-Powered-By: PHP/5.1.2
Set-Cookie: myimages=eef5cc5e36b051795891xxxxxxxx; expires=Fri, 14-Dec-2007 07:13:21 GMT; domain=.imageshack.us
Content-Type: text/html
Date: Tue, 19 Dec 2006 07:13:21 GMT
Server: lighttpd/1.4.8
Transfer-Encoding: chunked
----------------------------------------------------------
```

*3.Then use these api functions to set and get cookies accordingly.*
http://support.microsoft.com/kb/q196062/

I suggest you use inet control for this or the class module from vbip.com .

If needed try the user agent extension and modify headers extension to play around with headers.

----------


## kelceycoe

Thanks for such a wonderful script. How do I cite you as a credit for when I use it?

I got this error: 
HTTP/1.1 406 Not Acceptable
Date: Thu, 08 Feb 2007 19:12:41 GMT
Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a PHP-CGI/0.1b
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>406 Not Acceptable</TITLE>
</HEAD><BODY>
<H1>Not Acceptable</H1>
An appropriate representation of the requested resource /politicstestresults/post_dump.php could not be found on this server.<P>
<HR>
<ADDRESS>Apache/1.3.37 Server at www.kelceycoe.com Port 80</ADDRESS>
</BODY></HTML>

I've uploaded the files to my upload directory as shown, and chomodded the directory and php files as 777. Is that right or am I doing something way wrong? Thanks.

----------


## ashish ojha

Guys,

Me trying to upload file using VB HTTP Post method. Can successfully upload the files when my client machine is English OS . However , the problem occurs when am trying to upload the files from Chinese OS. It seems that the file uploaded is getting corrupted and could not be opened in correct format. This is applicable for binary files. The text files could be uploaded successfully.
I have tried setting content-type, charset,codepage etc properties to assemble the body.

Can anyone help me ? Its urgent ....Hope to find some VB GURU in this group.HELP !

Thanks in advance,
Ashish.0.












> Thanks for such a wonderful script. How do I cite you as a credit for when I use it?
> 
> I got this error: 
> HTTP/1.1 406 Not Acceptable
> Date: Thu, 08 Feb 2007 19:12:41 GMT
> Server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a PHP-CGI/0.1b
> Connection: close
> Content-Type: text/html; charset=iso-8859-1
> 
> ...

----------


## visualAd

It probably has something to do with the code page or character set used in Visual Basic. As I don't have a Chinese OS, I cannot test this. Your OS most probably uses Unicode (an encoding format not supported via HTTP).

For text files, the majority of characters will be within the extended ASCII range (<255). I would imagine what is happening with binary files, is that as each character is represented by 2 bytes instead of 1, the high order byte is being truncated and hence lost.

Have a look at this unicode FAQ posted by Merri.

http://www.vbforums.com/showthread.php?t=365738

In any case, I recommend that you convert all the strings to UTF-8 (the extended ASCII equivalent of Unicode) as this will ensure characters with codes above 256 are handled properly by the server.
My recommendation is that you convert the data to UTF-8 encoding.

----------


## ashish ojha

Umm.. thanks for the explaination ...tried ...but with little luck  :Frown: 
Anything you wish me to try ? me stuck with this issue.

----------


## try.test.abc

Hello
Your code is Working very well.
but now problem is that if i am Upload Single File It works Fine.
But If i am try to Upload Multiple file it only uploads the First one. 

What I am doing is Call Cmd_upload Multiple time to Upload Multiple File. Whether it is right way or not..


plz Help Me

----------


## Clooth

Could you help me make a program, which automatically takes a screenshot of the active window when hitting F11, then submits it to my own image uploader page and returns the url shown on it?

Regards,

Clooth

----------


## Trovamaster

Hi people,

I already try the code and modified to fit my requirements.

It work very well thanks to visualAd, but i have a little problem with the control winsockt, i duno how to ensure the control propely connect or disconect.

It works great, but at some point the control dosent disconect or conecct and the code hang up, into an infinite cilce, in part 1 or part 2 (listed in the code above)

i already try a lot of things, and of course i have 2 event's that change the flags for conected or disconected, on sckts events.

i hope somebody can help to figure out this issue.

Sorry for my very bad english.

this is part of the code:

            lngIndice = 1
            While lngIndice <= lngCantidadArchivos

                ' if the conection is un use, wait until disconected
                ' (part 1 when the control don't disconnect propely)
                If Not mbolSckConectado Then

                    ' Here i set variables for teh file
                    strNombreArchivo = arsNumerosOV(lngIndice) & ".PDF"
                    strArchivoPDF = strDirectorio & strNombreArchivo

                 ' here i get the contents of file
                 strContenidoArchivo=dfObtenerContenidoArchivo(strArchivoPDF)

                    ' here i biuld the HTTP request
                    strRequisicionHTTP = dfHTTPRequest(strContenidoArchivo, udtURLDestino, strNombreArchivo, strNombreArchivo, strTipoMIME)

                    ' here i conecct to the server, and wait until conected
                    ' (part 2 when the control don't connect propely)
                    sckSitioWeb.Connect
                    While Not mbolSckConectado
                        DoEvents
                    Wend

                    ' here i send the HTTP request
                    sckSitioWeb.SendData strRequisicionHTTP

                    ' here i susspend the program ejecution 2 sec
                    dfSuspenderEjecucion 2

                End If

            Wend

----------


## gsagie

Hi Guys, 

I would also would like to thank you for the great code!

As said before me, I also uploaded files successfully when the default language is set to English, but failed when I changed the default language to Chinese. 
Using snifer, I found out that the the HTTP packet sent by winsock.senddata is corrupted, and I dont understand why. The packet length was different when I used English and Chiense as default lanaguage, however the length of strHttp (in the line WinsockUpload.SendData strHttp) was identical in both scenarios.

Someone found a workaround/solution for that case?

Many thanks in advance

----------


## gerrymad

It seems to work great when sending it to the codedv.com site listed in the source code. When I try to send it to my own site I get a 403 error. I can upload files using a standard web form page, but I really need to be able to do it from VB and I was hoping this would be the solution.

Any suggestions?

----------


## WRYWERT

Please please please
Anyone here can convert this code to use Microsoft Internet Controls WebBrowser control ??..instead WinSock..
Please
WebBrowser control in Microsoft Internet Controls includes function Navigate and Navigate2, which can send navigate and post data and Headers usint it..
I tried, but  I couldn't make it work
So, please anyone can make it..really need that..

----------


## try.test.abc

> It probably has something to do with the code page or character set used in Visual Basic. As I don't have a Chinese OS, I cannot test this. Your OS most probably uses Unicode (an encoding format not supported via HTTP).
> 
> For text files, the majority of characters will be within the extended ASCII range (<255). I would imagine what is happening with binary files, is that as each character is represented by 2 bytes instead of 1, the high order byte is being truncated and hence lost.
> 
> Have a look at this unicode FAQ posted by Merri.
> 
> http://www.vbforums.com/showthread.php?t=365738
> 
> In any case, I recommend that you convert all the strings to UTF-8 (the extended ASCII equivalent of Unicode) as this will ensure characters with codes above 256 are handled properly by the server.
> My recommendation is that you convert the data to UTF-8 encoding.




I have a similar problem, but don't know at which place in vb coding i have to convert data to utf-8???

or can i use the following line in BuildFileUploadRequest() function

Content-Type: multipart-form-data;charset=utf-8, boundary=boundary

----------


## teguh123

Can anyone show me the VB code for the program? Also is there a good winhttp code for the program?

Finally, can we use webbrowser control to upload files onto web?

I noticed that webbrowser control also allow us to post directly on the site.

However we need to create the post requests first. Is there a fast way to decide that post requests?

----------


## libertad

> thanks a lot visualAd,
> it sound quite hard for that coding...
> me either didnt plan to do so...hehe...
> 
> im better choose another solution...
> im planning to update my sql server directly from my vb local client instead of upload the file then update at the server.
> 
> which one is better in terms of reliability?


Hi, If you  want add support for http authentication you could add that line in the source:


```
    ' construct the HTTP request in the form:
    '
    ' POST /path/to/reosurce HTTP/1.0
    ' Host: host
    ' Content-Type: multipart-form-data, boundary=boundary
    ' Content-Length: len(strbody)
    '
    ' HTTP request body
    strHttp = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
    strHttp = strHttp & "Host: " & DestUrl.Host & vbCrLf
    ' HTTP Authentication LINE BELOW HERE
    strHttp = strHttp & "Authorization: Basic YWRtaW4xNDptdXNpd2ViMDMwNg==" & vbCrLf
    strHttp = strHttp & "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf
    strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
    strHttp = strHttp & strBody

    BuildFileUploadRequest = strHttp
```

----------


## DigiRev

Thanks for the code visualAd. I was able to get most of it done myself before I saw this using a packet sniffer, but I was curious about the "boundaries". They can really be random?

Edit:
After reading more on it, boundaries are indeed random, and are nothing but delimiters that should be guaranteed to not appear in the files being uploaded (which would confuse the webserver).

----------


## visualAd

Yes,v as long as that sequence of characters appears nowhere in the file payloads.

----------


## mauren

hi, i need ur help. how to download http file using winsock? can u give me the visual basic 6 and php code? thx b4.

----------


## visualAd

> hi, i need ur help. how to download http file using winsock? can u give me the visual basic 6 and php code? thx b4.


I charge $50 per/hour for that service and a minimum fee of $300.  :wave:

----------


## timetell

Any way this code can be converted into Visual Basic 2010?

----------


## vicspainhower

Hello and hopefully youi're still monitoring this forum,

I'm trying to get the demo program to upload files to my website but it's not working for some reason.  I receive an HTTP Response of "HTTP/1.1 200 OK" but the test file (url.bas) doesn't show up in the HTTP Response window of the demo program and the file is not found on my website in the Uploaded_Files/Files directory. 

Here's what I'ver done (maybe you'll see what I've missed):

1. Created an example folder on my website and placed post_dump.php and uploaded_files.php in this folder.

2. Create uploaded_files folder and the files folder as a subfolder to the uploaded_files folder

3. Set Permissions of the Files folder to 777

4. Ran the demo program and tried to upload url.bas. Set the MIME Type to text/plain

5. Here's what is returned in the HTTP Response window:

HTTP/1.1 200 OK
Date: Wed, 15 Sep 2010 21:33:49 GMT
Server: Apache/2.2.16 (CentOS) mod_ssl/2.2.16 0.9.8l DAV/2 mod_fcgid/2.3.5 mod_auth_passthrough/2.1 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.13
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/htm

Thanks and I look forward to a reply from you!

Vic

----------


## vicspainhower

** Update ** - Well it never fails I ask for help and then I immediately find the answer.  Very strange, I thought I'd add some comments in the PHP code to see what was going on and I only added a startup message in the post_dump.php file and presto message displayed and the file uploaded.  Beats the heck out of me but it's now working fine.  Now I have to integrate this into the application where it's needed.

----------


## timetell

vicspainhower: you mind giving me the code you used?  I tried to use it on Vista x64 and x86 but I keep getting an error.  It works great in XP.

----------


## vb_programmer

There is no Progress bar added to this code. No way to check how many bytes transferred? I want to add Progress bar notification to the GUI. Any idea how can I achieve this?

----------


## haiflosse

Hi!
I have download the file_upload to send a file via http and vb6 to a webside.

It works wonderful but how can I change it to send more files.
Hope you can help me there what I must change.

kindly regards

----------


## Mike Trader

To implement HTTPS you will need to create an encrypted SSL session. There are two ways to do this at www.coastrd.com.

----------


## vicspainhower

Timetell - Hope this works for you - I'm using it on a Windows 7 64bit machine.



```

' Calling Routine

  URL = "http://maps.google.com/?f=q&source=s_q&hl=en&geocode=&q=http://www.website.com/GoogleMaps/uploaded_files/files/"
    
  URL = URL & SiteName & ".kml"
  kmlFile = SiteName & ".kml"
    
  URL = URL & "&sll=" & dLatitude & "," & dLongitude & "+++++++++++++*-----&sspn=0.104398,0.179043&layer=t&ie=UTF8&ll=" & dLatitude & "," & dLongitude
  URL = URL & "&spn=0.001148,0.001399&t=h&z=20&lci=com.panoramio.all"
  Call UploadRoutine(App.Path & "\GoogleEarth\" & kmlFile)
  ShellExecute Me.hwnd, "open", URL, vbNullString, "", 0

  exit sub
	

' this routine does all the work - it gathers the info required for the HTTP request
' and sends it via the winsock control
Private Sub UploadRoutine(FileName As String)
    Dim strFile As String
    Dim strHttp As String
    Dim DestUrl As URL
    
    ' if a request is allredy being sent
    ' exit
    If blnConnected Then Exit Sub
    
    ' check that a file was selected
    If FileName = vbNullString Then
        MsgBox "No File Chosen", vbCritical, "ERROR"
        
        Exit Sub
    End If

    ' extract the URL using a helper function
    DestUrl = ExtractUrl("http://www.perfected.com/GoogleMaps/post_dump.php")
    
    If DestUrl.Host = vbNullString Then
        MsgBox "Invalid Host", vbCritical, "ERROR"
        
        Exit Sub
    End If
    
    ' clear the old response
    txtResponse.Text = ""
    
    ' read the file contents as a string
    ' N.B: in HTTP everything is a string, even binary files
    strFile = GetFileContents(FileName)
    
    ' build the HTTP request
    strHttp = BuildFileUploadRequest(strFile, DestUrl, "File", FileName, "text/plain")
    
    ' assign the protocol host and port
    Winsock1.Protocol = sckTCPProtocol
    Winsock1.RemoteHost = DestUrl.Host
    
    If DestUrl.Port <> 0 Then
        Winsock1.RemotePort = DestUrl.Port
    Else
        Winsock1.RemotePort = 80
    End If
            
    ' make the connection and send the HTTP request
    Winsock1.Connect
    
    While Not blnConnected
        DoEvents
    Wend
    
    txtRequest = strHttp
    Winsock1.SendData strHttp
    DoEvents
End Sub


' this function builds a http request based on the following parameters:
'
' data = the data from the file to be uploaded
' DestUrl = a URL to containing information on where to send the data
' UploadName = the field upload name usually pass by <input type="file" name="uploadname"
' Filename = the name of the file
' The MIME type of the file

Private Function BuildFileUploadRequest(ByRef strData As String, _
                                        ByRef DestUrl As URL, _
                                        ByVal UploadName As String, _
                                        ByVal FileName As String, _
                                        ByVal MimeType As String) As String
    
    Dim strHttp As String ' holds the entire HTTP request
    Dim strBoundary As String 'the boundary between each entity
    Dim strBody As String ' holds the body of the HTTP request
    Dim lngLength As Long ' the length of the HTTP request
        
    ' create a boundary consisting of a random string
    strBoundary = RandomAlphaNumString(32)
    
    ' create the body of the http request in the form
    '
    ' --boundary
    ' Content-Disposition: form-data; name="UploadName"; filename="FileName"
    ' Content-Type: MimeType
    '
    ' file data here
    '--boundary--
    strBody = "--" & strBoundary & vbCrLf
    strBody = strBody & "Content-Disposition: form-data; name=""" & UploadName & """; filename=""" & _
                    FileName & """" & vbCrLf
    strBody = strBody & "Content-Type: " & MimeType & vbCrLf
    strBody = strBody & vbCrLf & strData
    strBody = strBody & vbCrLf & "--" & strBoundary & "--"
    
    ' find the length of the request body - this is required for the
    ' Content-Length header
    lngLength = Len(strBody)
    
    ' construct the HTTP request in the form:
    '
    ' POST /path/to/reosurce HTTP/1.0
    ' Host: host
    ' Content-Type: multipart-form-data, boundary=boundary
    ' Content-Length: len(strbody)
    '
    ' HTTP request body
    strHttp = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
    strHttp = strHttp & "Host: " & DestUrl.Host & vbCrLf
    strHttp = strHttp & "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf
    strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
    strHttp = strHttp & strBody

    BuildFileUploadRequest = strHttp
End Function
```

----------


## kashemali

i can Upload from here http://dev.mdimranhossain.com/uplod.php to my server with browser. But it doesn't work with this application with post method. when I submit with it the showing me 400 bad request. Please help me some one

----------


## iZz0s

Hi !

This script is working great for me  :Smilie: 

Is it maybe possible to add some post variables to that script ?

if someone can help me with that, all is working 100% for me  :Big Grin:

----------


## SaLiXeM

Sorry for bumping an old thread.
But I need a progress bar added to this.
Can anyone help me please? I don't have any ideas about how it can be done.

----------

