Upload file without user interaction - VBA

  • Thread starter Thread starter google.com
  • Start date Start date
G

google.com

Hi !

I would like to upload files to a Webserver, but the problem is that
the server only accepts uploads via FORM. The form looks like this:

<form action="upload.asp?
action=upload&type=product&item=pic1&element=&id=3170&w=1000&h=1000&maxw=200&maxh=5000"
method="post" name="upload" enctype="multipart/form-data">
<input type="hidden" name="type" value="product">
<input type="hidden" name="item" value="pic1">
<input type="hidden" name="element" value="">
<input type="hidden" name="id" value="3170">
<input type="hidden" name="w" value="1000">
<input type="hidden" name="h" value="1000">
<input type="file" name="picture" size="28">
<input type="submit" value=" upload "">
</form>

So my question is, how do I create a sub in vba/excel, which looks
like this:

public sub UploadFile( FileToUpload as string)

The sub should read the file FileToUpload, and post it on the
webserver using the form.

Any help appreciated, because I have to upload 1000+ files 8-()


/hco
 
I'm not an expert on this, but I think you arre a little confused in what you
are asking for. I think you need to transfer a file, not send the text data
line by line. You probably need to use FTP, not HTTP.

FTP is File Transfer Protocol. HTTP is Hyper-Text Transfer Protocol. HTTP
sends the text character inside a file and displays the text to a screen.
FTP just send the files between two computers but doesn't display the text.

Your posting first says I want to upload files which is FTP.
 
The OP does not seem confused to me - requirements seem clear that the only
available method is using an HTTP form and the POST method. The question is
then how to automate this process.

Never tried this, but one possibility might be to automate IE to upload the
files. The on sticking point may be that for obvious reasons the "file"
input element does not allow setting the value (path) through script: not
sure if this extends to automation from outside of IE.

Tim
 
Excel may be the right tool for the job when it comes to saving the
file names you want to upload, but it's the wrong tool for the job
when it comes to posting those files to a url using http.

The right tool for the job would be H-P's LoadRunner application.
You'd open ViewGen, turn on the recorder, visit the form, upload a
file, edit the generated code to fetch file names from your Excel
Workbook, open the Controller, then create a task to upload all your
files.

You can download a test value and if it's a one shot deal go with
that.

If this happens on a regular basis, then LR might not be the tool for
the job because it has an expensive license.

In anycase, the fact that you aren't using ftp or aren't mounting a
shared drive suggests that either you're a spammer or you don't know
what you're doing.
 
Hi there!

I've been digging around and found the following article covering the
area:

http://www.motobit.com/tips/detpg_uploadvbaie/

It describes the vba code required to handle a very simple upload
form:

<Form Action=http://127.0.0.30/util/up/free/upload.asp
Method=Post ENCTYPE="multipart/form-data">
<Input Type=File Name=FileField>
<Input Type=Submit>
</Form>

And then the VBA comes here:

'******************* upload - begin
'Upload file using input type=file
Sub UploadFile(DestURL As String, FileName As String, _
Optional ByVal FieldName As String = "File")
Dim sFormData As String, d As String

'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary As String =
"---------------------------0123456789012"

'Get source file As a string.
sFormData = GetFile(FileName)

'Build source form with file contents
d = "--" + Boundary + vbCrLf
d = d + "Content-Disposition: form-data; name=""" + FieldName +
""";"
d = d + " filename=""" + FileName + """" + vbCrLf
d = d + "Content-Type: application/upload" + vbCrLf + vbCrLf
d = d + sFormData
d = d + vbCrLf + "--" + Boundary + "--" + vbCrLf

'Post the data To the destination URL
IEPostStringRequest DestURL, d, Boundary
End Sub

'sends URL encoded form data To the URL using IE
Sub IEPostStringRequest(URL As String, FormData As String, Boundary As
String)
'Create InternetExplorer
Dim WebBrowser: Set WebBrowser =
CreateObject("InternetExplorer.Application")

'You can uncoment Next line To see form results
'WebBrowser.Visible = True

'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(FormData) - 1)
bFormData = StrConv(FormData, vbFromUnicode)

WebBrowser.Navigate URL, , , bFormData, _
"Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf

Do While WebBrowser.busy
' Sleep 100
DoEvents
Loop
WebBrowser.Quit
End Sub

'read binary file As a string value
Function GetFile(FileName As String) As String
Dim FileContents() As Byte, FileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
FileNumber = FreeFile
Open FileName For Binary As FileNumber
Get FileNumber, , FileContents
Close FileNumber
GetFile = StrConv(FileContents, vbUnicode)
End Function
'******************* upload - end


It almost works, but I stil haven't found out how to pass all the
parameters on the form, as I've stated in my initial post.

So, I also think excel is the tool, but I just need some extra
tweaking ;-)

So any suggestions??

/hco
 
See if you can access Explorer's document object and set the form
parameters with the syntax:

document.myForm.myParameter ="myValue"
 
That won't do the job, because the above code is actually mimicking
the stream send from the browser to the webserver.
In other words, I never actually load the form. Only submit the data.

No, I think the secret lies in the Encoding in the UploadFile(DestURL
As String, FileName As String,...) sub.
My problem is that I just don't know how to encode other fields into
the formdata.

But maybe this is more like a ASP question and not VBA question. Any
good groups for that??

/hco
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top