Note that there are some explanatory texts on larger screens.

plurals
  1. POShow image upload preview with Lift
    primarykey
    data
    text
    <p>I want to upload an image with Lift and show it immediatly in some element of the page, without reloading. I want to do it like in <a href="http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html" rel="nofollow">this</a> solution (which uses PHP).</p> <p>The process there is comparatedly simple. I replaced the PHP file with a dummy HTML snippet:</p> <pre><code>&lt;form id="imageform" method="post" enctype="multipart/form-data" action="ajaximage.html"&gt; &lt;input id="imageFile" name="imageFile" type="file" size="50" maxlength="100000"/&gt; &lt;/form&gt;&lt;br/&gt; &lt;script src="jquery-1.8.3.min.js"&gt;&lt;/script&gt; &lt;script src="jquery.form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#imageFile').live('change', function() { $("#preview").html(''); $("#preview").html('&lt;img src="images/processing.jpg" alt="Uploading...."/&gt;'); $("#imageform").ajaxForm({ target : '#preview' }).submit(); }); }); &lt;/script&gt; </code></pre> <p>The HTML snippet:</p> <pre><code>&lt;img src="images/someimage.jpg"/&gt; </code></pre> <p>Well, this works. But I need dynamic behaviour obviously. The example page is using instead of my HTML snippet a PHP file, which extracts the data from the request, saves it in the server, and outputs the HTML for the preview image accordingly - <code>echo "&lt;img src='uploads/".$actual_image_name."' class='preview'&gt;";</code></p> <p>Now, I have no idea how to accomplish this with Lift.</p> <p>I found this discussion: <a href="https://groups.google.com/forum/?fromgroups=#!topic/liftweb/6bxNlbBPJvE" rel="nofollow">https://groups.google.com/forum/?fromgroups=#!topic/liftweb/6bxNlbBPJvE</a> But no good solution there besides a vague advice for a framework and HTML5 things which I don't want to use.</p> <hr> <p><strong>Part 2</strong>. I have, already implemented some basics for this in Lift:</p> <p>HTML:</p> <pre><code>&lt;div class="lift:AddImage.addEntry" form="POST" multipart="true"&gt; &lt;p&gt;&lt;prefix:imageUpload/&gt;&lt;/p&gt; &lt;p&gt;&lt;prefix:test/&gt;&lt;/p&gt; &lt;/div&gt; </code></pre> <p>snippet:</p> <pre><code>package code package snippet import net.liftweb.http.S import net.liftweb.common.Full import net.liftweb.common.Empty import net.liftweb.common.Box import net.liftweb.http.FileParamHolder import net.liftweb.util._ import Helpers._ import scala.xml.Group import scala.xml.NodeSeq import net.liftweb.http.SHtml import javax.annotation.Resource import java.io.OutputStream import java.io.FileOutputStream class AddImage { // Add a variable to hold the FileParamHolder on submission var fileHolder : Box[FileParamHolder] = Empty def submitTest () { println("submitTest called!") val receiptOk = fileHolder match { // An empty upload gets reported with a null mime type, // so we need to handle this special case case Full(FileParamHolder(_, null, _, _)) =&gt; true case Full(FileParamHolder(_, mime, _, data)) =&gt; println("MIME: " + mime) if (mime.startsWith("image/")) { println("uploading the image: ") //scala.io.Source.fromBytes(data) try { val out: OutputStream = new FileOutputStream("test.jpg"); out.write(data); out.flush(); out.close(); } catch { case e: Exception =&gt; println("Exception!: " + e) } true } case Full(_) =&gt; { S.error("Invalid attachment") false } case _ =&gt; true } // (e.validate, receiptOk) match { // } } def addEntry(content: NodeSeq): NodeSeq = { bind("prefix", content, "imageUpload" -&gt; SHtml.fileUpload(f =&gt; fileHolder = Box !! f), "test" -&gt; SHtml.submit("submitz", submitTest)) } } </code></pre> <p>But I'm stuck here because:</p> <ul> <li><p>I don't know how to reference the form created by Lift from jquery.</p></li> <li><p>The javascript submits the form when the upload input field changes, with <code>ajaxForm()</code> and inserts the result of the PHP script in the <code>#preview</code> element. I don't know how to get the form submit in scala and further output the image HTML, such that it can be inserted in <code>#preview</code></p></li> </ul> <p>Hope this is not to much questions :) thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload