【问题标题】:Upload Document to Alfresco using C# (Html form POST, no CMIS)使用 C# 将文档上传到 Alfresco(Html 表单 POST,无 CMIS)
【发布时间】:2019-06-25 20:51:13
【问题描述】:

露天文档没有很好地解释它期望什么样的 HTTP 请求才能执行上传。

谁能解释一下如何使用简单的 HTTP 请求而不是 CMIS 将文档上传到 Alfresco DMS?

【问题讨论】:

    标签: c# alfresco alfresco-enterprise


    【解决方案1】:

    露天 api http://YOURALFRESCOHOST/alfresco/service/api/upload 期望: multipart/form-data 将通过 HTTP Post 请求发送。

    因此,该服务期望使用带有 <form> 标记的老式 html 页面。含义数据将通过表单发布在 html 中的默认方式发送。我猜这是为了简化创建您自己的文档上传屏幕的过程。

    当然,这只是另一个 http 请求,因此可以通过 C# 或任何其他编程语言模拟 post 操作。

    谢天谢地,从 .NET 4.5 开始,我们有了可用于此确切目的的 MultipartFormDataContent 类。请参考下面的示例代码来执行您的上传:

    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}");
        }
    }
    

    如果上传成功,你会看到这样的响应:

    {
       "nodeRef": "workspace://SpacesStore/38238e6f-e9d9-4158-a3ce-8a13d0962348",
       "fileName": "test.txt",
       "status":
       {
          "code": 200,
          "name": "OK",
          "description": "File uploaded successfully"
       }
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 5.2 或更高版本,请始终检查 API 资源管理器以查看已经存在的内容以及不错的文档:

      https://api-explorer.alfresco.com/api-explorer/#!/nodes/updateNodeContent

      之后,您可以转到本地安装以查看已安装的所有 Web 脚本(适用于任何 Alfresco 版本):

      http://localhost:8080/alfresco/service/

      之后,浏览网页和 SO :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        相关资源
        最近更新 更多