Saving files to history using ODATA

We have an implementation of saving data in the "History" of Contacts, and we use the HttpWebRequest method like this POST. However, now every time we try to upload a file into the History email it returns this  Error: 'The remote server returned an error: (400) Incorrect Request.'.

private static bool TransferFile(Guid fileRecordId, string columnName, byte[] file) {
            log.Debug("[START] Transfer file");
            try {
                string requestUri = ServerUriUsr + "ActivityFileCollection(guid'" + fileRecordId.ToString() + "')/" + columnName;
                HttpWebRequest request = RequestHelper.BuildRequest(requestUri, HttpMethod.Put);
                request.Accept = "application/octet-stream,application/json;odata=verbose";
                request.ContentType = "multipart/form-data;boundary=+++++";
                // Recording the xml message to the request stream.
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(file, 0, file.Length);
                }
                // Receiving a response from the service regarding the operation implementation result.
                using (WebResponse response = request.GetResponse())
                {
                    if (((HttpWebResponse)response).StatusCode == HttpStatusCode.Created)
                    {
                        // Processing the operation implementation result.
                        response.Close();
                        log.Debug("[END] Transfer file (Successful)");
                        return true;
                    }
                }
            }
            catch (WebException ex) {
                if (ex.Response is null)
                    log.Error($"TransferFile function error: {ex.Message}");
                else
                    log.Error($"TransferFile function error: {new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()}");
                throw;
            }
            catch (Exception ex) {
                log.Error($"TransferFile function error: {ex.Message}");
                throw;
            }
 
            return false;
        }

The problem only occurs when I try to upload a .docx or .xlsx file

Like 0

Like

2 comments

Have you checked failed IIS requests logs and the applicaiton logs for more information on the invalid request? What was the result of the check?

Oleg Drobina,

I ended up doing more tests and the problem only occurs when it is a .docx and .xlsx type file.

When it is PDF or images it works normally without any problems.

Show all comments