2016-06-15 62 views
0

我與MongoDB的問題,我有一個Java SOAP Web服務,我的前臺在Angular/Bootstrap/Jquery中,我需要在我的mongo數據庫中保存一個文件,但我不知道如何做這個,我正在嘗試這個爲期3天,我需要一個JQuery/js調用SOAP服務來做這個或者直接使用js的例子,我接收到的文件是<input type="file" id="file"/>。 謝謝你的幫助。 我的文件類型是帶有json和圖像的tgz,比我需要保存在我的mongo數據庫中的類型要多。在MongoDB中保存文件

function uploadCard() { 
    var fileList = new Array(); 
    fileList = document.getElementById("file").files; 
    var fr = new FileReader(); 
    if (FileReader && fileList && fileList.length) { 
     fr.readAsArrayBuffer(fileList[0]); 
     imageData = fr.result; 
    } 

    jQuery.support.cors = true; 
    var wsUrl = "http://" + window.location.hostname + ":7004/CardServiceImpl/CardService?WSDL"; 
    var soapRequest = crateCardSoapRequest(document.getElementById("file").files[0]); 
    $.ajax({ 
     crossDomain: true, 
     type: "POST", 
     url: wsUrl, 
     contentType: "text/xml", 
     dataType: "xml", 
     data: soapRequest, 
     success: processSuccess, 
     error: processError 
    }); 
    } 

    function crateCardSoapRequest(input) { 

    var req = "<soap:Envelope xmlns:soap=" + '"' + "http://schemas.xmlsoap.org/soap/envelope/" + '"' + ">" + 
      "<soap:Body>" + 
      "<ns1:uploadCard xmlns:ns1=" + '"' + " http://card.ejb.card_editor.com.br/" + '"' + ">" + 
      "<arg0>" + 
      "<cardBean>"+ 
    "<description>StringValue</description>" + 
      "<id>23</id>" + 
      "<name>StringValue</name>" + 
      "<file>" + input + "</file>" + 
      "<template>" + "</template>" + 
      "</cardBean>" + 
      "<nickNameUser>lucas_santos</nickNameUser>" + 
      "</arg0>" + 
      "</ns1:uploadCard>" + 
      "</soap:Body>" + 
      "</soap:Envelope>"; 

    } 

我的肥皂參數的java.io.File

+2

你說你已經嘗試了三天。你在那段時間嘗試了哪些方法,以及遇到了什麼問題? –

+0

那麼你使用的是Angular還是jQuery?無論如何,數據庫不會存儲文件。他們將URL存儲到文件中。 –

+0

我嘗試傳遞soap參數中的文件,並且ajax請求無法識別我的方法,我認爲比我需要轉換爲二進制數組或字符串,但我沒有成功完成此操作。 –

回答

0

數據庫是爲了存儲數據的類型。您可能不應該將文件(如圖像等)直接粘貼到數據庫中。相反,請將文件上傳到某個存儲服務器或CDN並鏈接到您的數據庫。

然後,您查詢數據庫,找到URL,然後從該URL獲取資源。

+0

好的,這是做這件事的其他方法,但我的項目的建議是將數據存儲在數據庫中,mongoDb支持這一點。 –

+0

我如何做到這一點,你有一個存儲服務器的圖像爲我指示? –