The alfresco documentation doesn't do a good job explaining what kind of HTTP request it's expecting in order to perform an upload.
Can someone please explain how to upload a document to Alfresco DMS using simple HTTP requests and not CMIS?
The alfresco api http://YOURALFRESCOHOST/alfresco/service/api/upload
expects:
multipart/form-data
to be sent via HTTP Post requests.
So the service is expecting an old school html page with a <form>
tag to be used. Meaning data will be sent via the default way of how form posting works in html. I'm guessing this was made this way in order to simplify the process of creating your own document upload screens.
Of course under the hood it's just another http request so a post operation can be simulated via C# or any other programming language.
Thankfully starting with .NET 4.5 we have the MultipartFormDataContent class that can be used for this exact purpose. Refer to the sample code below to perform your uploads:
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StreamContent(File.Open("test.txt", FileMode.Open)), "filedata", "test.txt");
formData.Add(new StringContent("mysiteid"), "siteid");
formData.Add(new StringContent("mycontainerid"), "containerid");
formData.Add(new StringContent("/"), "uploaddirectory");
formData.Add(new StringContent("test"), "description");
formData.Add(new StringContent("cm:content"), "contenttype");
formData.Add(new StringContent("true"), "overwrite");
var response = client.PostAsync("http://YOURALFRESCOHOST/alfresco/service/api/upload?alf_ticket=TICKET_XXXXXXXXXXXXXXXXXXXXXXXXX", formData).Result;
string result = null;
if (response.Content != null)
{
result = response.Content.ReadAsStringAsync().Result;
}
if (response.IsSuccessStatusCode)
{
if (string.IsNullOrWhiteSpace(result))
result = "Upload successful!";
}
else
{
if (string.IsNullOrWhiteSpace(result))
result = "Upload failed for unknown reason";
}
Console.WriteLine($"Result is: {result}");
}
}
You'll see a response like this if the upload was successful:
{
"nodeRef": "workspace://SpacesStore/38238e6f-e9d9-4158-a3ce-8a13d0962348",
"fileName": "test.txt",
"status":
{
"code": 200,
"name": "OK",
"description": "File uploaded successfully"
}
}