Haskell how to parse file uploads/multipart form data using Hack? - haskell

I'm creating a simple Hack2 app, and I can read body data with:
directory :: Application
directory env = do
body <- input_bytestring env
...
I'm trying to switch my form to use file uploads
<form action="/directory" method="POST" enctype='multipart/form-data'>
<div><input type="file" name="data"></div>
<div><input type="submit"></div>
</form>
But it's giving me a ShortWriteException. Maybe input_bytestring can't handle multipart. Is there a library that can handle multipart form data? Any examples of doing this with Hack2?

I never did figure this out. I switched to Happstack-lite, because I couldn't figure this out, and it seems like nobody is using Hack2.

Related

SLIM and TWIG error

I have this error
An exception has been thrown during the rendering of a template
("Named route does not exist for name: auth.signup").
This is the TWIG code for the NAV template
<li>Sign up</li>
This is the ROUTE definition
$app->group('/auth', function () {
$this->get('/signup', 'App\Controllers\Auth\AuthController:getSignup')
->setName('auth.signup');
$this->post('/signup', 'App\Controllers\Auth\AuthController:postSignup');
$this->get('/signin', 'App\Controllers\Auth\AuthController:getSignin')
->setName('auth.signin');
$this->post('/signin', 'App\Controllers\Auth\AuthController:postSignin');
});
I'm stumped because the SIGNIN template code works just fine
<form action="{{ path_for('auth.signup') }}" method="post" autocomplete="off">
Any ideas?
I found the issue. I'm new to this and I thought was being cleaver... to cleaver for my own good. I had my route collections in separate files and would only load the route asked for. Seems that TWIG needs the containers that hold PATH_FOR values as well. I put all the routes in a single file and it works fine

Javascript Nesting Quotes not clear

What will be the Nesting Quotes for this
"javascript:window.open('http://localhost:9000/index.html#/mypage/detailspage\',
'details-page','width=300,height=250');"
I tried this not working
"javascript:window.open(\'http://localhost:9000/index.html#/mypage/detailspage\',
\'details-page\',\'width=300,height=250\');"
var y="javascript:window.open('http://localhost:9000/index.html#/mypage/detailspage\' ,\'details-page\',\'width=300,height=250\'')";
alert(y);
This works fine in browser i guess.
Thanks guys for the reply but it is now working for me. Actually my requirement is that from my app I am passing the value as a post request to another app in that app I am not able to see my code as I am sending it. Below is the example
<input type="hidden" name="value11" id="value11" ng-model="indexedArray[34]" value="javascript:window.open('http://localhost:9000/index.html#/abc/details-page\' ,\'details-page\',\'width=300,height=250\'');" autocomplete="off" class="ng-pristine ng-untouched ng-valid ng-empty">
In response I am getting this

How to display progress of the file upload using node.js in real time

I am working on the project which simply read the txt/csv files from the directory and store in the database.
The upload function works like a charm but the problem is that I want to display the progress - any notification that tells the users that the system is working on the files.
Here is my HTML markup
<div class="form">
<input type="file" id="uploadFile" />
<button id="submit" type="button">Upload</button>
</div>
<div class="terminal">
<! -------------------RESULT---------------------->
</div>
I have the div called terminal to display the progress of file uploading. The way I place text inside the div is to use the innerHTML function of javascript. Thus, my upload code is as follows
var files = fs.readdirSync(folder_path);
// Loop through files gathered from folder reading
for(i=0; i<files.length; i++) {
document.getElementbyId("terminal").innerHTML+=files[i]+"<br/>";
var fileData = fs.readfileSync(folder_path+'\'+files[i], 'utf8');
// Upload function below
}
The above markup is a part of my js file which serves as an uploader. I first read the folder with fs.readdirSync, then iterate through the array of files gathered from readdirSync function. I want to display the file name in each loop in real time.
But it just displays everything once the upload has finished. So I don't know how to make it display the progress in real time or in other word, just like the console which displays any information in real time.
You don't have to loop, you can easily port this to your frontend app:
Monitoring XHR Progress
https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress

Nodejs query for uploading photos

I am new to nodejs. I am basically using express, and I have to create a form to upload photos. So far I have created a file called upload.handlebars where I have written the following code:
<form enctype="multipart/form-data" action="uploads" method="post">
<input type="file", id = "image", name="image", accept="image/*">
<input type='submit' id='tags' value='Upload'>
I have another file called router.js where I have written the post function. One of the lines in the post function is:
fs.readFile(req.files.image.path, function (err, data) {
However, I get an error as follows:
TypeError: Cannot read property 'image' of undefined at Object.handle....
How should I specify the 'name' of the image file in my router.js?
Make sure you have multiparty middleware enabled.
https://github.com/andrewrk/connect-multiparty
Latest express doesn't come with it.
you can use express to solve this problem quickly. to know more read the folowing page express api reference
for now the following code may help you -
app.post('<your path>',express.bodyParser(),function(req,res){
console.log(req.files);//to get all the files uplaoded
});
make sure express version should 3.4

client/server approach in Meteor

I am wondering if something as basic as client/server operation model is easily doable with Meteor (meteor.com) framework.
A primitive template should
<template name="input">
<div>
<input type="text" val="">
<input type="submit">
</div>
</template>
<template name="output">
<div id="output">
</div>
</template>
wait for input, calls a server to execute serverFunction() on the input's value, and insert the result into the output tag. No collections, mongo, or authentication required. Of course, every client is supposed to receive its own results. It seems that Meteor.publish() operates only on collections.
Have a look at the Methods section in Meteor documentation:
http://docs.meteor.com/#methods_header
"Methods are remote functions that Meteor clients can invoke."
There's also code in the Wordplay example to show how to work this RPC mechanism (see the definition of Meteor.methods({ ... }) in model.js and game.js in this example project for more info).
a Meteor Collection is only associated with a Mongo collection if you tell it to be.. you can also use them to wrap arbitrary data.
The three main benefits of Meteor Collections (for me, at least):
the publish / subscribe model for keeping server and client data in sync
they can wrap persisted (Mongo) or arbitrary data
the ability to query them with Mongo-like syntax
This is what I was looking for:
server:
Meteor.methods =
doStuff: (input) ->
serverFunction input
client:
Template.input.events =
'submit': ->
Meteor.call 'doStuff', $('input[type=text]').val(), (error, result) -›
Session.set 'result', result
Template.output.output = ->
Session.get 'result

Resources