/// <summary> /// Create Attachment data object using the give parameters. /// </summary> /// <param name="name">Attachment name.</param> /// <param name="content">Attachment content url.</param> /// <param name="contentType">Attachment content type.</param> /// <returns>A valid AttachmentData instance.</returns> private async Task <AttachmentData> CreateAttachmentDataAsync(string name, string content, string contentType) { if (string.IsNullOrEmpty(contentType)) { throw new ArgumentNullException(nameof(contentType), "Content type can not be null."); } if (string.IsNullOrEmpty(content)) { throw new ArgumentNullException(nameof(content), "Content url can not be null."); } // ContentUrl can contain a url or dataUrl of the form "data:image/jpeg;base64,XXXXXXXXX..." byte[] bytesData; if (AttachmentHelper.IsUrl(content)) { bytesData = await _wechatClient.SendHttpRequestAsync(HttpMethod.Get, content, timeout : 60000).ConfigureAwait(false); } else { bytesData = AttachmentHelper.DecodeBase64String(content, out contentType); } name = name ?? Guid.NewGuid().ToString(); // should be lower by WeChat. #pragma warning disable CA1308 contentType = contentType.ToLowerInvariant(); #pragma warning restore CA1308 return(new AttachmentData(contentType, name, bytesData, bytesData)); }