FlickrNet.Flickr.UploadPicture C# (CSharp) Method

UploadPicture() public method

UploadPicture method that does all the uploading work.
public UploadPicture ( Stream stream, string title, string description, string tags, int isPublic, int isFamily, int isFriend ) : string
stream Stream The object containing the pphoto to be uploaded.
title string The title of the photo (optional).
description string The description of the photograph (optional).
tags string The tags for the photograph (optional).
isPublic int 0 for private, 1 for public.
isFamily int 1 if family, 0 is not.
isFriend int 1 if friend, 0 if not.
return string
        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);
            }
        }

Same methods

Flickr::UploadPicture ( string filename ) : string
Flickr::UploadPicture ( string filename, string title ) : string
Flickr::UploadPicture ( string filename, string title, string description ) : string
Flickr::UploadPicture ( string filename, string title, string description, string tags ) : string
Flickr::UploadPicture ( string filename, string title, string description, string tags, bool isPublic, bool isFamily, bool isFriend ) : string

Usage Example

Exemplo n.º 1
3
        public IPhoto UploadPhoto(Stream stream, string filename, string title, string description, string tags)
        {
            using (MiniProfiler.Current.Step("FlickrPhotoRepository.UploadPhoto"))
            {
                Flickr fl = new Flickr();

                string authToken = (ConfigurationManager.AppSettings["FlickrAuth"] ?? "").ToString();
                if (string.IsNullOrEmpty(authToken))
                    throw new ApplicationException("Missing Flickr Authorization");

                fl.AuthToken = authToken;
                string photoID = fl.UploadPicture(stream, filename, title, description, tags, true, true, false,
                    ContentType.Photo, SafetyLevel.Safe, HiddenFromSearch.Visible);
                var photo = fl.PhotosGetInfo(photoID);
                var allSets = fl.PhotosetsGetList();
                var blogSet = allSets
                                .FirstOrDefault(s => s.Description == "Blog Uploads");
                if (blogSet != null)
                    fl.PhotosetsAddPhoto(blogSet.PhotosetId, photo.PhotoId);

                FlickrPhoto fphoto = new FlickrPhoto();
                fphoto.Description = photo.Description;
                fphoto.WebUrl = photo.MediumUrl;
                fphoto.Title = photo.Title;
                fphoto.Description = photo.Description;

                return fphoto;
            }
        }
All Usage Examples Of FlickrNet.Flickr::UploadPicture
Flickr