如何使用.Net的rest API在Adobe AEM JCR中添加图像文件

时间:2016-04-22 11:37:40

标签: aem jcr

我google了很多但没有获得任何有关restfull API的Adobe AEM文档。我试过了.Net代码 https://helpx.adobe.com/experience-manager/using/using-net-client-application.html。 但它创建文件夹而不是上传内容。 我们需要传递什么参数来上传任何图像,mp4,pdf等。下面是我的c#代码。

protected void Button1_Click(object sender, EventArgs e)
{
    string fileName=FileUpload1.FileName;

    String postURL = "http://localhost:4502/content/dam/geometrixx/" + fileName;

    System.Uri uri = new System.Uri(postURL);
    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(uri);
    NetworkCredential nc = new NetworkCredential("admin", "admin");

    httpWReq.Method = "POST";
    httpWReq.Credentials = nc;
    httpWReq.ContentType = "application/x-www-form-urlencoded";
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = FileUpload1.FileBytes;
    httpWReq.ContentLength = data.Length;

    using (System.IO.Stream stream = httpWReq.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

    string responseText = string.Empty;

    using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
    {
        responseText = reader.ReadToEnd();
    }

    TextBox1.Text = responseText;
}

2 个答案:

答案 0 :(得分:0)

我不熟悉.Net,但问题更多的是如何在AEM中创建资产。您没有指定任何版本,因此我仅在AEM 5.6.1上测试了我的代码,但它也适用于AEM 6.X.在下面的代码片段中,您可以看到如何使用curl将新文件上传到您选择的大坝文件夹中,因此您只需通过.Net代码拨打电话:

curl -u admin:admin -X POST -F file=@"/absolute/path/to/your/file.ext" http://localhost:4502/content/dam/the/path/you/wish/to/upload/myfolder.createasset.html

您正在向Dam路径发送POST请求,其中必须使用选择器上传文件" createasset"和扩展" html"。

答案 1 :(得分:0)

用于在Aem上传文件的.net代码。请尝试以下代码。

var filelocation = AppDomain.CurrentDomain.BaseDirectory + "Images\\YourFile with Extension";

FileStream stream = File.OpenRead(filelocation);
byte[] fileBytes = new byte[stream.Length];

stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();


var httpClientHandler = new HttpClientHandler()
{
    Credentials = new NetworkCredential("admin", "Your Password"),
};

//var httpClient = new HttpClient(httpClientHandler);
using (var httpClient = new HttpClient(httpClientHandler))
{
    var requestContent = new MultipartFormDataContent();
    var imageContent = new ByteArrayContent(fileBytes);
    requestContent.Add(imageContent, "file", "file nmae with extension");


    var response1 = httpClient.PostAsync("http://siteDomain/content/dam/yourfolder.createasset.html", requestContent);
    var result = response1.Result.Content.ReadAsStringAsync();

}