Domanda

I'm having some issue with uploading file attachments for News in Valence API. I'm using PHP and when I try to make a post request, the action never gets completed. I'm running it on command line so I have to kill the process to terminate the program.

My file data and details(Name, Type and Size) are stored in the database and I'm using just one call to post everything i.e. the News in JSON Format and the file attachments. I'm using the following format for the post message (As mentioned in the API docs):

Content-Type: multipart/mixed; boundary=e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Length: 46538

--e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Type: application/json

{"Title":"Some News","Body":{"Text":"This </span>is a very </span>colorful title</span>","Html":"This </span>is a very </span>colorful title</span>"},"StartDate":"2013-11-06T05:00:00.000Z","EndDate":"2013-11-30T05:00:00.000Z","IsGlobal":false,"IsPublished":true,"ShowOnlyInCourseOfferings":false}

--e9e1c17c958e2a89a192bf5d3f7d1ffd Content-Disposition: form-data; name="FC.png"; filename="FC.png"

Content-Type: image/png

{} --e9e1c17c958e2a89a192bf5d3f7d1ffd--

I can't see the file content due to encoding I guess. I'm calculating the content-length in this message as: string length of the JSON data + filesize in bytes.

My very speicific question is that can one post files from the database directly or it's recommended to write the file to the disk first then try posting. I would also appreciate if someone can point me to an example online where there's a pre-existing sample code using PHP for this.

One more thing. If I do not include the file-size in bytes for the content-length, then it works fine except for the file attachment. It is 29 bytes in size and when I click on it, it opens with corrupt data error.

Any kind of help will be appreciated. Thanks.

È stato utile?

Soluzione 2

Found what I was doing wrong. Needed to UTF encode the file document data before using json_encode on it. The data was getting lost during json_encode as it does not understands special characters. Code snippet:

$fileData = array(
    'FileName' => $attachment->getDocumentFilename(),
    'FileType' => $attachment->getDocumentFileType(),
    'FileSize' => $attachment->getDocumentSizeByte(),
    'FileData' => utf8_encode($attachment->getDocument())
);

Altri suggerimenti

The Content Length of the HTTP request must be the length (byte-count) of the entire HTTP body. This means the JSON structure's size, the number of bytes in the file-stream, as well as the length of all surrounding body-part headers/separators and whitespace.

There's no particular requirement about the data store where your uploaded file data comes from. Essentially, you're forming an HTTP request with multiple parts in the Body: the first part must be the JSON structure (of media type application/json); the second part should be the bytes for the "file", and the media type for that body part set appropriately image/png or whatever) -- whether you gather that byte stream from a database, or from a file, doesn't really matter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top