How to send file using in servresource in Ajax callHow to send file using in servresource in Ajax callhttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=478717562024-03-30T03:41:25Z2024-03-30T03:41:25ZRE: How to send file using in servresource in Ajax callFernando Fernandezhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1173403542019-10-02T14:47:08Z2019-10-02T14:47:08ZHi all<br /><br />I'm currently using serveResource to upload files and other AJAX stuff, including sending and receiving data to the server side. The reason is that it's the only way, I think, to send data without triggering a page refresh - re-rendering all portlets in the page. It might not be the intended use of serveResource but it works well and it's fast.<br /><br />HTH<br /><br />FernandoFernando Fernandez2019-10-02T14:47:08ZRE: How to send file using in servresource in Ajax callDavid H Nebingerhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=480389242015-01-22T13:37:12Z2015-01-22T13:37:12Z<div class="quote-title">Jeet Chatterjee:</div><blockquote>I did not get you David.Please be more specific i am new to liferay</blockquote><br /><br />This has nothing to do with Liferay, this is the basic JSR 286 specification.<br /><br />In the specification, there are 4 types of methods a portlet can handle: action, render, resource, and event. Each type has a separate purpose, action is for handling user activity, render is for rendering the html fragment that will be part of a page, resource is for serving resources back to the browser (i.e. fetching ajax data, building images, document files, etc.), and event is for IPC based upon event handling.<br /><br />So you're trying to force the resource type to all of a sudden act like it is another action type, which it is not.<br /><br />Check out the definition: <a href="http://www.oracle.com/technetwork/java/jsr286-2-141964.html#Serving_Resources">http://www.oracle.com/technetwork/java/jsr286-2-141964.html#Serving_Resources</a>.<br /><br />What you're doing is, effectively, misusing serveResource by passing in arguments to build a dynamic resource by giving a file to be uploaded. This is not how the serveResource was meant to be used and if you actually could get it working, it would only work because of a misuse of the specification.<br /><br />New to liferay is no excuse to be misusing the specification. Instead of just sitting down and try to code something up, I'd suggest learning more about the portal specification and how things should be handled.David H Nebinger2015-01-22T13:37:12ZRE: How to send file using in servresource in Ajax callJeet Chatterjeehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=480207002015-01-22T06:17:13Z2015-01-22T06:17:13ZI did not get you David.Please be more specific i am new to liferayJeet Chatterjee2015-01-22T06:17:13ZRE: How to send file using in servresource in Ajax callVipin Bardiahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=479632172015-01-21T06:03:10Z2015-01-21T06:03:10ZHi Jeet,<br /><br />Are you not able to send data with - data:{lname:lname,fname:fname} ?<br /><br />Then you can do what david suggested, add param in your url.<br /><br />Regards.Vipin Bardia2015-01-21T06:03:10ZRE: How to send file using in servresource in Ajax callDavid H Nebingerhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=479461952015-01-20T18:49:19Z2015-01-20T18:49:19ZIf this works it is merely a fluke. The JSR specification never meant for the serve resource request to be used for a file upload.<br /><br />As far as passing additional data, why not use url parameters?David H Nebinger2015-01-20T18:49:19ZRE: How to send file using in servresource in Ajax callJeet Chatterjeehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=479300972015-01-20T14:47:50Z2015-01-20T14:47:50Z<html><head></head><body>Thank for replying my post,I have successfully sended file to the server with the help of you people. But i stuck in some where now,<br><br>I can send the file to the server using this ajaxFileUpload ,but i want to send all the data along with it i am getting from the textboxes.How to send all the data to the server along with the file name can anyone help. am doing this<br><pre><code>
<portlet:resourceurl var="candidateInterestURL" id="candidateInterestURLID" />
var fname = $('#<portlet:namespace />fname').val();
var lname = $('#<portlet:namespace />lname').val();
$.ajaxFileUpload({
url:'&lt;%=candidateInterestURL%&gt;',
secureuri : false,
fileElementId : 'fileName',
dataType : 'json',
data:{lname:lname},
success : function(data, status) {
alert('hiii');
},
error:function (data, status, e){
alert("There might be some issue with this file.");
}
});
</code></pre><br><br>Can anyone help??</body></html>Jeet Chatterjee2015-01-20T14:47:50ZRE: How to send file using in servresource in Ajax callDavid H Nebingerhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=479307282015-01-20T13:36:27Z2015-01-20T13:36:27ZCan you provide the definition for candidateInterestURL?<br /><br />It's important to understand you cannot upload a file using the serveResource URL, it can only be a post to an action URL.<br /><br />serveResource is only for returning resources via ajax.<br /><br />Sorry if this is actually far away from what you're trying to ask, but mixing serve resource with file upload is fairly confusing.David H Nebinger2015-01-20T13:36:27ZRE: How to send file using in servresource in Ajax callJeet Chatterjeehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=479194712015-01-20T09:55:33Z2015-01-20T09:55:33ZI have tried this but not working for me .. <img alt="emoticon" src="@theme_images_path@/emoticons/sad.gif" ><br /><br /><br /><script type="text/javascript"><br /><br /> function sendToConsultant(){<br /> <br /> var url = "<%=candidateInterestURL%>";<br /> <br /> var fname = $('#<portlet:namespace />fname').val();<br /> var lname = $('#<portlet:namespace />lname').val();<br /> var email = $('#<portlet:namespace />email').val();<br /> var phone = $('#<portlet:namespace />phone').val();<br /> var jobtitle = $('#<portlet:namespace />jobtitle').val();<br /> var comments = $('#<portlet:namespace />comments').val();<br /> var type = "candidateInterestInJob";<br /> <br /> <br /> jQuery.getJSON(url+"&type="+type+"&fname="+fname+"&lname="+lname+"&email="+email+"&phone="+phone+"&jobtitle="+jobtitle+"&comments="+comments, function(data) {<br /> <br /> }); <br /> <br /> <br /> <br /> $.ajaxFileUpload({<br /> <br /> url:'<%=candidateInterestURL%>',<br /> secureuri : false,<br /> fileElementId : 'fileName',<br /> dataType : 'json',<br /> <br /> success : function(data, status) {<br /> <br /> alert(fileElementId);<br /> }<br /> }); <br /> <br /> <br /> }<br /><br /></script><br /><div id="candidateInfo"><br /><div id="candidateInfoBanner"><br />Connect with Our Consultant<br /><hr class="hrinJobDetails"><br /></div><br /><form method="post" enctype="multipart/form-data" name="form" id=" " ><br /><table><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="fname" id="fname" type="type" placeholder="First Name" size="40" /></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="lname" id="lname" type="type" placeholder="Last Name" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="email" id="email" type="type" placeholder="Email" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="phone" id="phone" type="type" placeholder="Phone No." size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxNonMandetory" label="" name="jobtitle" id="jobtitle" type="type" placeholder="Title" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxNonMandetory" label="" type="textarea" id="comments" rows="5" cols="44" name="comments" placeholder="Your Comments"/></td><br /></tr><br /><tr><br /><%-- <td><aui:input inlineField="true" type="file" name="fileName" label="Upload your CV. Supported Formats: doc, docx, rtf, pdf." size="40" accept=".doc,.docx,.rtf,.pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/rtf,application/pdf"/></td> --%><br /><td><input type="file" name="fileName" id="fileName" value="Browse" /></td><br /></tr><br /><tr><br /><td><br /><br /><aui:button type="submit" name="Submit" cssClass="applyJobToConsultant" value="Send" style="width: 160px" onClick="sendToConsultant()"></aui:button><br /></td><br /></tr><br /></table> <br /></form><br /></div><br /><br />In the Action class<br /><br /><br />else if(type.equals("candidateInterestInJob")){<br /> <br /> <br /> System.out.println("Reached");<br /> String firstName = null;<br /> String lastName = null;<br /> String emailOfCandidate = null;<br /> String phoneNo = null;<br /> String candidateJobTitle = null;<br /> String candidateComments = null;<br /> UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);<br /> <br /> <br /> firstName = ParamUtil.getString(uploadRequest,"fname");<br /> lastName = ParamUtil.getString(uploadRequest,"lname");<br /> emailOfCandidate = ParamUtil.getString(uploadRequest,"email");<br /> phoneNo = ParamUtil.getString(uploadRequest,"phone");<br /> candidateJobTitle = ParamUtil.getString(uploadRequest,"jobtitle");<br /> candidateComments = ParamUtil.getString(uploadRequest,"comments");<br /> //File resume = uploadRequest.getFile("fileName");<br /> <br /> <br /> System.out.println(firstName+lastName+emailOfCandidate+phoneNo+candidateJobTitle+candidateComments);<br /> InputStream inputStream = uploadRequest.getFileAsStream("fileName");<br /> System.out.println("-------------");<br /> if(Validator.isNotNull(inputStream)){<br /> File file = FileUtil.createTempFile(inputStream);<br /> System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>."+file);<br /> String uploadString = getFileAsString(file);<br /> System.out.println(">>>>>>>"+uploadString);<br /> <br /> if (Validator.isNotNull(uploadString)) {<br /> <br /> resourceResponse.getWriter().write( "validated_successfully");<br /> <br /> <br /> <br /> }Jeet Chatterjee2015-01-20T09:55:33ZRE: How to send file using in servresource in Ajax callMeera Princehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=478726822015-01-19T14:14:01Z2015-01-19T14:14:01ZHI <br />it may help you..<br /><br /> <a href=""> http://michi-path.blogspot.in/2013/02/file-upload-using-serveresource-ajax.html</a><br /><a href=""> https://www.liferay.com/community/forums/-/message_boards/message/31536959</a><br /><br /><br />Regards,<br />Meera PrinceMeera Prince2015-01-19T14:14:01ZRE: How to send file using in servresource in Ajax callAyatullah Abdulhakimhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=478719552015-01-19T13:58:00Z2015-01-19T13:58:00ZYou cannot upload any file using ajax, you may use any js library that can help you.<br /><br />https://blueimp.github.io/jQuery-File-Upload/basic.html<br /><br />or <br /><br />https://github.com/davgothic/AjaxFileUploadAyatullah Abdulhakim2015-01-19T13:58:00ZRE: How to send file using in servresource in Ajax callVipin Bardiahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=478725052015-01-19T13:52:58Z2015-01-19T13:52:58Z<html><head></head><body>Hi Jeet,<br><br>You can take a reference from below code - <br><br><pre><code>
$.ajaxFileUpload({
url:ajaxUrl,
secureuri:false,
fileElementId:elementId,
dataType: 'json',
success: function (data, status)
{
// Success
},
error: function (data, status, e)
{
alert("There might be some issue with this file.");
}
});
</code></pre><br><br>Reference :: http://www.phpletter.com/Demo/AjaxFileUpload-Demo/<br><br>Regards.</body></html>Vipin Bardia2015-01-19T13:52:58ZHow to send file using in servresource in Ajax callJeet Chatterjeehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=478717552015-01-19T13:32:11Z2015-01-19T13:32:11ZI am using servresource to send parameters to action all fields i have sccessfully submitted by i have one upload button there in which i have to attach CV of some candidate.i want to send the file using that servesource call to the action.But the way i am doing it ,getting null every time .Here is my code ...<br /> <br /><br /><script type="text/javascript"><br /><br /> function sendToConsultant(){<br /> <br /> var fname = $('#<portlet:namespace />fname').val();<br /> var lname = $('#<portlet:namespace />lname').val();<br /> var email = $('#<portlet:namespace />email').val();<br /> var phone = $('#<portlet:namespace />phone').val();<br /> var jobtitle = $('#<portlet:namespace />jobtitle').val();<br /> var comments = $('#<portlet:namespace />comments').val();<br /> var fileName = $('#<portlet:namespace />fileName');<br /> var type = "candidateInterestInJob";<br /> var url = "<%=candidateInterestURL%>";<br /> alert(fileName)<br /> jQuery.getJSON(url+"&type="+type+"&fname="+fname+"&lname="+lname+"&email="+email+"&phone="+phone+"&jobtitle="+jobtitle+"&comments="+comments+"&fileName="+fileName, function(data) {<br /> <br /> });<br /> <br /> }<br /><br /></script><br /><br /><br /><br /> <table><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="fname" id="fname" type="type" placeholder="First Name" size="40" /></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="lname" id="lname" type="type" placeholder="Last Name" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="email" id="email" type="type" placeholder="Email" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxJobDetails" label="" name="phone" id="phone" type="type" placeholder="Phone No." size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxNonMandetory" label="" name="jobtitle" id="jobtitle" type="type" placeholder="Title" size="40"/></td><br /></tr><br /><tr><br /><td><aui:input cssClass="textBoxNonMandetory" label="" type="textarea" id="comments" rows="5" cols="44" name="comments" placeholder="Your Comments"/></td><br /></tr><br /><tr><br /><td><aui:input inlineField="true" type="file" name="fileName" label="Upload your CV. Supported Formats: doc, docx, rtf, pdf." size="75" accept=".doc,.docx,.rtf,.pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/rtf,application/pdf"/></td><br /></tr>