I would like my webservice to be 开发者_StackOverflow中文版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
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
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
which user runs my asp.net user?
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?
精彩评论