2016-04-22 66 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(); 

} 
相關問題