public string UploadPicture(Stream stream, string title, string description, string tags, int isPublic, int isFamily, int isFriend)
{
/*
*
* Modified UploadPicture code taken from the Flickr.Net library
* URL: http://workspaces.gotdotnet.com/flickrdotnet
* It is used under the terms of the Common Public License 1.0
* URL: http://www.opensource.org/licenses/cpl.php
*
* */
string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss");
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(UploadUrl);
req.UserAgent = "Mozilla/4.0 FlickrNet API (compatible; MSIE 6.0; Windows NT 5.1)";
req.Method = "POST";
if( Proxy != null ) req.Proxy = Proxy;
//req.Referer = "http://www.flickr.com";
req.KeepAlive = true;
req.Timeout = HttpTimeout * 1000;
req.ContentType = "multipart/form-data; boundary=" + boundary + "";
req.Expect = "";
StringBuilder sb = new StringBuilder();
Hashtable parameters = new Hashtable();
if( title != null && title.Length > 0 )
{
parameters.Add("title", title);
}
if( description != null && description.Length > 0 )
{
parameters.Add("description", description);
}
if( tags != null && tags.Length > 0 )
{
parameters.Add("tags", tags);
}
if( isPublic >= 0 )
{
parameters.Add("is_public", isPublic.ToString());
}
if( isFriend >= 0 )
{
parameters.Add("is_friend", isFriend.ToString());
}
if( isFamily >= 0 )
{
parameters.Add("is_family", isFamily.ToString());
}
parameters.Add("api_key", _apiKey);
parameters.Add("auth_token", _apiToken);
string[] keys = new string[parameters.Keys.Count];
parameters.Keys.CopyTo(keys, 0);
Array.Sort(keys);
StringBuilder HashStringBuilder = new StringBuilder(_sharedSecret, 2 * 1024);
foreach(string key in keys)
{
HashStringBuilder.Append(key);
HashStringBuilder.Append(parameters[key]);
sb.Append("--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n");
sb.Append("\r\n");
sb.Append(parameters[key] + "\r\n");
}
sb.Append("--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"api_sig\"\r\n");
sb.Append("\r\n");
sb.Append(Md5Hash(HashStringBuilder.ToString()) + "\r\n");
// Photo
sb.Append("--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"photo\"; filename=\"image.jpeg\"\r\n");
sb.Append("Content-Type: image/jpeg\r\n");
sb.Append("\r\n");
UTF8Encoding encoding = new UTF8Encoding();
byte[] postContents = encoding.GetBytes(sb.ToString());
byte[] photoContents = new byte[stream.Length];
stream.Read(photoContents, 0, photoContents.Length);
stream.Close();
byte[] postFooter = encoding.GetBytes("\r\n--" + boundary + "--\r\n");
byte[] dataBuffer = new byte[postContents.Length + photoContents.Length + postFooter.Length];
Buffer.BlockCopy(postContents, 0, dataBuffer, 0, postContents.Length);
Buffer.BlockCopy(photoContents, 0, dataBuffer, postContents.Length, photoContents.Length);
Buffer.BlockCopy(postFooter, 0, dataBuffer, postContents.Length + photoContents.Length, postFooter.Length);
req.ContentLength = dataBuffer.Length;
Stream resStream = req.GetRequestStream();
int j = 1;
int uploadBit = Math.Max(dataBuffer.Length / 100, 50*1024);
int uploadSoFar = 0;
for(int i = 0; i < dataBuffer.Length; i=i+uploadBit)
{
int toUpload = Math.Min(uploadBit, dataBuffer.Length - i);
uploadSoFar += toUpload;
resStream.Write(dataBuffer, i, toUpload);
if( (OnUploadProgress != null) && ((j++) % 5 == 0 || uploadSoFar == dataBuffer.Length) )
{
OnUploadProgress(this, new UploadProgressEventArgs(i+toUpload, uploadSoFar == dataBuffer.Length));
}
}
resStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
XmlSerializer serializer = _uploaderSerializer;
StreamReader sr = new StreamReader(res.GetResponseStream());
string s= sr.ReadToEnd();
sr.Close();
StringReader str = new StringReader(s);
FlickrNet.Uploader uploader = (FlickrNet.Uploader)serializer.Deserialize(str);
if( uploader.Status == ResponseStatus.OK )
{
return uploader.PhotoId;
}
else
{
throw new FlickrException(uploader.Error);
}
}