How to upload images in jaggeryjs?

272 views Asked by At

This is my sample HTML code

<html>
        <head>
            <title>
                fileupload
            </title>
        </head>
        <body>
            <form action="process.jag" method="post" id="" name="">
                  <!-- File input filed -->
                  <input type="file" name="myFile">

                  <!-- The submit button. It calls the server side function uploadfiles() on click -->
                  <input type="submit" id="" value="Upload File">

            </form>
        </body>
    </html>

This is my jaggery code

<%
    var verb=request.getMethod();
    if (verb=='POST'){

        var log = new Log();
        var file = request.getFile("myFile");
        file.move("C:\Users\vatsal ramesh gosar\Desktop\New folder\form\fileUploadUsingJaggery"+file.getName());

        response.sendRedirect("https://www.google.com");

    }

%>

It is giving a NULL Pointer Exception. Please help me solve this problem with an efficient code.

2

There are 2 answers

0
nifCody On

In Jaggery documentation I haven't find any way to upload image into the server. But some alternatives there...

You encode the image into base64 encoding, you may use this way too encode the images on base64

$path = 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

then send the encoded String using POST request using Jaggery.

0
SajithA On

request.getFile("myFile") does not represent a physical file, rather it represent a stream to the uploaded file. Hence you cannot "move" it using file.move("/moving/path") function. If you want to save the uploaded file, then use the following code snippet.

var uploadedFile = request.getFile("myFile");
var savedFile = new File("/path/to/save/location/" + uploadedFile.getName());
savedFile.open('w');
savedFile.write(uploadedFile.getStream());
savedFile.close();