Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Save Attachment from System.IO.Stream #138

Open
dcastro273 opened this issue Jan 1, 2022 · 0 comments
Open

Save Attachment from System.IO.Stream #138

dcastro273 opened this issue Jan 1, 2022 · 0 comments

Comments

@dcastro273
Copy link

Please add a new AttachFile method for streams to SheetAttachmentResourcesImpl.

Below is the method. I don't have a working build of the source code to implement myself.

`private Attachment AttachFile(string path, string fileName, string contentType, Stream stream)
{
Utility.Utility.ThrowIfNull(fileName);
Utility.Utility.ThrowIfNull(stream);

        if (contentType == null)
        {
            contentType = "application/octet-stream";
        }

        HttpRequest request = CreateHttpRequest(new Uri(this.Smartsheet.BaseURI, path), HttpMethod.POST);

        request.Headers["Content-Disposition"] = "attachment; filename=\"" + fileName + "\"";

        HttpEntity entity = new HttpEntity();
        entity.ContentType = contentType;

        MemoryStream ms = new MemoryStream();
        stream.CopyTo(ms);

        entity.Content = ms.ToArray();
        entity.ContentLength = fi.Length;
        request.Entity = entity;

        HttpResponse response = this.Smartsheet.HttpClient.Request(request);

        Attachment attachment = null;
        switch (response.StatusCode)
        {
            case HttpStatusCode.OK:
                attachment = this.Smartsheet.JsonSerializer.deserializeResult<Attachment>(
                    response.Entity.GetContent()).Result;
                break;
            default:
                HandleError(response);
                break;
        }

        this.Smartsheet.HttpClient.ReleaseConnection();

        return attachment;
    }`

Thanks,

Dan

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant