File Upload Web Service

  • Thread starter Thread starter Alphonse Giambrone
  • Start date Start date
A

Alphonse Giambrone

I am trying to develop a web service to accept a file uploaded via http post
from a vba (Access) application.
Thanks to helpful links in a vb newsgroup and my previous work with WinInet
api I was able to write the code to do the upload. I know this works because
I tested by posting to an asp page that saves the file.
Now I need to create a .NET web service to accept and save the file. Of all
the web service samples I found, the only ones that deal with file upload
receive the file as a byte array from another .net object (like a webform).
How can I access the http post and extract the uploaded file in a web
service.
Or if that is not possible, how about from a aspx page?

TIA
 
Hi Alphonse,

From your description, you are wanting to generate a webserivie or a
webform page which can help process the uploaded file postd from a
client(OFFICE VBA ?).

As for this question, here are my suggestions:
1. If use WebService, it is general that we define a certain webmethod
which takes a byte array param and a string param such as
pubic void UploadFile(bytes as Byte() , filename as String)

And then, we can easily call it in .NET application since we can use the
WSDL.EXE or the VS.NET to generate a easytouse client proxy class.

However, since you'd like to call the webservice in VBA application, I
think two mean in VBA can help to call webservice:
a) use MXXML COM component to send raw XML soap message, this will have us
to write the soap http message manually and post it via XMLHTTP component.

b) There is a webservice add-in in OFFICE which can help to generate a
webservice proxy. I think this will be more convenient. You may have a
search in MSDN to see whether the certain ADD-IN will help.


2. In addition to webservice, I think a simple asp.net webform page is also
capable of handling client uploaded file. The ASP.NET 's butildin
Request(HttpRequest class) object has a "Files" member which can help to
handle the client uploaded files. Here are some references on the
HttpRequest.Files member and handle upload files in ASP.NET:
#HttpRequest.Files Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebhttprequest
classfilestopic.asp?frame=true

#Uploading Files Using the File Field Control
(this article is focus on use webform to upload, but you can also get ideas
on how to handle the file steam from other clients)
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-fileupload.asp?fr
ame=true

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi Alphonse,

For Office XP & 2003 VBA Web service development, Microsoft has released Office web service Toolkit which will provide some assistance on your
scenario.
Microsoft Office 2003 Web Services Toolkit 2.01
http://www.microsoft.com/downloads/...8a-e1cf-48a3-9b35-169d819ecf18&DisplayLang=en

Microsoft Office XP Web Services Toolkit 2.0
http://www.microsoft.com/downloads/...0F-002A-4F5B-AF74-978F2CD6C798&displaylang=en

Some MSDN technique articles will introduce more info regarding this.
Microsoft Office XP Web Services Toolkit
http://msdn.microsoft.com/library/en-us/dnxpwst/html/odc_wstoolkitoverview.asp

Please feel free to let me know if you have any further questions.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks both for the replies.

The Office toolkits are not an option. I should have mentioned that I am
using vba with wininet api for the upload because I need the solution to
work with Access 97 and up without any additional references or installs. It
is pretty safe to assume my users have IE and therefore wininet.dll.

It seems that you are saying I can't post the file to a web service, but can
to a web page. This would be ok if need be, but I would prefer using a web
service.
Am I understanding this correctly?
 
You'll have to remember that a webservice uses SOAP/XML for all method
post/requests. While you can certainly encode a file to send through a web
service (e.g. a byte array, or otherwise) I think you'll find much better
built in support to older office files for doing a more standard file post
over http.

You can create a very basic web page... just a class that descends from the
Page class, and on the Page load event, you can check for the files being
uploaded. It sounds like you already have that working on the office/client
side -- getting it to work on the server side should be easy.

I wonder, then, why you are wanting to use the web service? I guess
Microsoft has done a lot of marketing work on how great web services are --
but if the client can't easily consume the web service, it's not much use.

That's just my two cents.

--
Mike Mayer, C# MVP
(e-mail address removed)
http://www.mag37.com/csharp/
 
Thanks for your response Mike.
Yes, you are correct. After all I have read about web services, it just
seemed like the right thing to use since the process will not require any
human intervention other than to agree to the upload. Plus, I have also read
that a web service can accept http get and post, not just SOAP/XML.
Once I get the upload process working I will need to implement some security
and thought it might be easier using a web service to pass a token to my vba
app which would then include it with the file to provide authentication.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
 
Thanks for Mike's informative suggestions.

Hi Alphonse,

As for the "a web service can accept http get and post, not just SOAP/XML."
you mentioned, here is my understanding on this:

The HTTP POST /GET or SMTP are the actual transport protocal the webservice
message binded to, it can be defined and described in the WSDL of the
webservice. And the SOAP/XML is the message format(how the content are
formatted in the message) of the webservice, they're difference from each
other. In other word, SOAP/XML is the message infrastructure, which is
based on the actual transport protocal, http post or http get or smtp. ....

---------------------------------------------------------
SOAP/XML message body //abstracted high
level
---------------------------------------------------------
http get | http get | smtp | other protocal //fundamental
implement

Do you think so? In addition, as for implementing authentication mechanism
in webservice, there're many buildin approachs in ASP.NET webservice such
as integrated with IIS's authentication. Or defind some customer SOAP
headers to provide the authentication token. Also, you can provide
authentication token in every method call, that's the simplest means. :).
Below is a former post discussing the authentication in webservide in
communty:

# Web Service Security
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=9xA7M#
THEHA.3800%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fq%3Dwebservice%2Bsec
urity%2Bsteven%2Bcheng%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks for the help Steven,

I was finally able to assemble what I wanted. I found that I could access
the uploaded files in a web service just the same as a web form.
It turned out to be so incredibly easy, I wonder if I am missing something.
I found that the Request.Files collection is accessible in both a web form
and web service. Don't know why I could not find any articles spitting that
out.
So, basically, I created a global function that has an HttpFileCollection as
an input parameter. I simply pass the Request.Files collection from either
my user web form or my 'automated' (for posting from my vba app) web form
to the class, which then saves the file(s) and notifies me via email.
I decided to use a web form for the vba post just so I would not need to
maintain another project for one simple task, but it would work just the
same if I had used a web service.
For security with my vba post, I concatenate login and password to the file
name (since my understanding is that additional data can not be included
with file posts), parse it out in my new function and check authentication
using all the same functions for normal site user login.
The 'automated' web form then sends a structured response that the vba app
can parse out to determine it the post was successful.

BTW, the article at
http://msdn.microsoft.com/library/e...mwebhttprequestclassfilestopic.asp?frame=true
is incorrect.
It states that the example "writes out the names of all files in the Files
collection".
What it actually writes out is the 'key' names.
To get the actual file name one must use:
Request.Files(arr1(loop1)).FileName

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
 
Hi Alphonse,

Thanks for the followup. One further suggestion based on the following
things you mentiond:
==========================
I found that the Request.Files collection is accessible in both a web form
and web service. Don't know why I could not find any articles spitting that
out.
==========================

I'm not sure whether you've written some code to test it, I haven't tried
such means before. Yes, we do can access the buildin Request object in both
webform or webservice applicatoin. However, they are quite different from
each other fundamentally. The Request object in ASP.NET represent the Http
Web Reqeust from the client.

IN ASP.NET webservice, the Reqeust represent the raw http WebRequest
because the webservice message is bind to HTTP generally. However, the
message content of the webservice is SOAP/XML format. In fact, the
webservice provide us a remote method call mode which make the client as
calling a method on the server machine rather than post a raw http Request
stream to the target page in asp.net web application. So it is recommend
that we use Request.Files collectoin in webform based application rather
than webservice application. Do you think so? Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks for the input Steven.

I did in fact test out receiving files via a web service and it works quite
well. Although as I mentioned, in production, I am using a web form.

Here is the actual code if you want to try it.

<WebMethod()> _

Public Function SaveFiles() As String

Dim loop1 As Integer

Dim arr1() As String

Dim Files As HttpFileCollection

Dim memFile As HttpPostedFile

Dim bytReq(0) As Byte

Dim intLen As Integer

Dim strParam As String

Files = HttpContext.Current.Request.Files ' Load File collection into
HttpFileCollection variable.

arr1 = Files.AllKeys ' This will get keys (not file names as incorrectly
stated in MSDN article) of all files into a string array.

Dim strResult As String

Dim strFname As String

Dim strParams() As String

For loop1 = 0 To arr1.GetUpperBound(0)

strResult &= "File control: " & Server.HtmlEncode(arr1(loop1)) & vbCrLf

memFile = HttpContext.Current.Request.Files(arr1(loop1))

strFname = memFile.FileName

strResult &= "File Name: " & strFname & vbCrLf

memFile.SaveAs("F:\Webs\CDSDotNet\Upfiles\" &
System.IO.Path.GetFileName(strFname))

strParams = Split(strFname, vbTab)

Next loop1

For Each strParam In strParams

strResult &= vbCrLf & strParam

Next

Return strResult

End Function

You can post to it from a simple html page containing one or more html file
controls.

I am using my vba app and capturing the response.
Here is the response from the web service after posting 3 files:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/CDSServices/UploadFile">File control:
file0
File Name: F:\AmerEagle\APR.mdb
File control: file1
File Name: F:\Temp\TestFile.txt
File control: file2
File Name: F:\Temp\flash_animation.js

F:\Temp\flash_animation.js</string>

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
 
Back
Top