views:

47

answers:

2

I would like my webservice to be able to take a username and a binary file parameter in the same call to it. Is this possible?

I thought I could use WebClient.Credentials and set a username that way, but it doesnt seem to be working

+1  A: 

The simplest way would be to turn your binary data into a base64 string. The string can be transmitted by passing it as a parameter or inside an XML string.

Web Service Method:

Now depending on the size files, this will incur a performance loss, but not a substantial one.

To convert a byte array to base64 string:

byte[] data = GetSomeData(...);//Whatever you use to get a byte array.
string sData = System.Convert.ToBase64String(data );

I will choose this method over WebClient.FileUpload() because I have more control and more options. Rather than just post to a page, I can send additional information up with this file, usernames, passwords etc. Especially if I put them all into a neat XML File.

[WebMethod]
public bool SubmitFile(string userName, string file)
{
}

or

[WebMethod]
public bool SubmitFile(string data)
{
}

where data could be:

<data>
 <authentication>
   <userName></userName>
   <password></password>
 </authentication>
 <files>
   <file>
     <!-- Base64 String goes here -->
   </file>
 </files>
</data>

You can choose to keep the file as a byte array, and just pass that, but I'm always a fan of using XML in webservices, so including my file inside an XML snippet (easier to log and record SOAP information).

 [WebMethod]
    public void PutFile(byte[] buffer, string filename, string userName) {
        BinaryWriter binWriter = new BinaryWriter(File.Open(Server.MapPath(filename),
           FileMode.CreateNew, FileAccess.ReadWrite));
        binWriter.Write(buffer);
        binWriter.Close();
    }

WebClient.FileUpload method.

You will want to use cookies here. You will need to somehow authenticate with the server who you are (if you go this route) or at least have a UserName cookie the server will understand.

When you send your file, make sure you have the appropriate cookie going along with your file so the server knows what to do.

An example : http://www.codeproject.com/KB/cs/uploadfileex.aspx

Ryan Ternier
Dont I then lose the simplicity of using WebClient.UploadFile()? I basically want to be able to upload a file, and make an association to a particular user in my web service
Derek
What do you mean by “64-bit string”? A Base64-encoded string maybe?
Joey
@Joey - yes a base64String. Edited and fixed :) thanks for catching that.
Ryan Ternier
I ended up using the WevClient.FileUload and creating an overridden version of WebClient that was cookie aware
Derek
A: 

If you want to Authenticate or to get access:

The Credentials are used to authenticate/impersonate the request via IIS. remember to impersonate the identity in the web.config

http://stackoverflow.com/questions/3735678/which-user-runs-my-asp-net-user/3859696#3859696

otherwise I would have thought publishing a method which provides 2 params

[WebMethod]
public void UploadFile(string user, byte[] file)
{
}  

could you post your attempts?

dbones