简体   繁体   中英

How to upload images in jaggeryjs?

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.

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM