Exemplo n.º 1
0
        /// <summary>
        /// Uploads a file to zendesk and returns the corresponding token id.
        /// To upload another file to an existing token just pass in the existing token.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="token"></param>
        /// <returns></returns>       
        string UploadAttachment(ZenFile file, string token="")
        {
            var requestUrl = _zenDeskUrl;
            if (!requestUrl.EndsWith("/"))
                requestUrl += "/";

            requestUrl += string.Format("uploads.xml?filename={0}", file.FileName);
            if (!string.IsNullOrEmpty(token))
                requestUrl += string.Format("&token={0}", token);

            WebRequest req = WebRequest.Create(requestUrl);
            req.ContentType = file.ContentType;
            req.Method = "POST";
            req.ContentLength = file.FileData.Length;
            var credentials = new System.Net.CredentialCache
                                  {
                                      {
                                          new System.Uri(_zenDeskUrl), "Basic",
                                          new System.Net.NetworkCredential(_user, _password)
                                          }
                                  };

            req.Credentials = credentials;
            req.PreAuthenticate = true;
            req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
            var dataStream = req.GetRequestStream();
            dataStream.Write(file.FileData, 0, file.FileData.Length);
            dataStream.Close();

            WebResponse response = req.GetResponse();
            dataStream = response.GetResponseStream();
            var reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();

            return GetAttachmentToken(responseFromServer);
        }
Exemplo n.º 2
0
 public string UploadAttachment(ZenFile file)
 {
     return UploadAttachment(file, "");
 }