Exemplo n.º 1
0
        private static async Task <Content> UploadTextInternalAsync(string text, UploadData uploadData, ODataRequest requestData,
                                                                    CancellationToken cancellationToken, ServerContext server = null)
        {
            // force set values
            if (text.Length > ClientContext.Current.ChunkSizeInBytes)
            {
                throw new InvalidOperationException($"Cannot upload a text that longer than the chunk size " +
                                                    $"({ClientContext.Current.ChunkSizeInBytes}).");
            }
            if (uploadData.FileLength == 0)
            {
                uploadData.FileLength = text.Length;
            }
            uploadData.FileText = text;

            dynamic uploadedContent = null;

            var model       = JsonHelper.GetJsonPostModel(uploadData.ToDictionary());
            var httpContent = new StringContent(model);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue(JsonContentMimeType);

            await ProcessWebResponseAsync(requestData.ToString(), HttpMethod.Post, server, httpContent,
                                          async response =>
            {
                if (response != null)
                {
                    var rs          = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    uploadedContent = JsonHelper.Deserialize(rs);
                }
            }, cancellationToken).ConfigureAwait(false);

            if (uploadedContent == null)
            {
                return(null);
            }

            int contentId = uploadedContent.Id;
            var content   = Content.Create(contentId);

            content.Name = uploadedContent.Name;
            content.Path = uploadedContent.Url;

            return(content);
        }
Exemplo n.º 2
0
        private static HttpWebRequest CreateChunkUploadWebRequest(string url, ServerContext server, UploadData uploadData, string boundary, out Stream requestStream)
        {
            var myRequest = GetRequest(url, server);

            myRequest.Method      = "POST";
            myRequest.ContentType = "multipart/form-data; boundary=" + boundary;

            myRequest.Headers.Add("Content-Disposition", "attachment; filename=\"" + Uri.EscapeUriString(uploadData.FileName) + "\"");

            var boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            // we must not close the stream after this as we need to write the chunk into it in the caller method
            requestStream = myRequest.GetRequestStream();

            //write form data values
            foreach (var kvp in uploadData.ToDictionary())
            {
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);

                var formitem      = string.Format(UPLOAD_FORMDATA_TEMPLATE, kvp.Key, kvp.Value);
                var formitembytes = Encoding.UTF8.GetBytes(formitem);

                requestStream.Write(formitembytes, 0, formitembytes.Length);
            }

            //write a boundary
            requestStream.Write(boundarybytes, 0, boundarybytes.Length);

            //write file name and content type
            var header      = string.Format(UPLOAD_HEADER_TEMPLATE, "files[]", Uri.EscapeUriString(uploadData.FileName));
            var headerbytes = Encoding.UTF8.GetBytes(header);

            requestStream.Write(headerbytes, 0, headerbytes.Length);

            return(myRequest);
        }