2016-04-22 63 views
1

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

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

回答

0

我不熟悉.Net,但问题是更多关于如何在AEM中创建资产。您没有指定任何版本,因此我仅在AEM 5.6.1上测试了我的代码,但它也应该在AEM 6.X上运行。下面的代码片段你可以看到如何使用卷曲到您选择在坝体的文件夹上传新的文件,所以你只有从.NET代码拨打电话:

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

您发送POST向选择器“createasset”和扩展名“html”必须上传文件的dam路径请求。

+1

我没有规定使用卷曲。有没有可能给一个例子上传资产使用简单的JavaScript? –

+0

您的问题与JavaScript和客户端代码执行无关。如果你仍然需要通过JS上传文件,你可以检查[这个链接](http://blog.teamtreehouse.com/uploading-files-ajax)。您要求的是如何使用AEM Rest API上传文件,我认为我给了您所需的一切。你有网址,方法和解释。 – d33t

0

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

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(); 

} 
相关问题