Thread: how to insert stream into table using nodejs?
Hello all,
i'm looking for a way to insert a file into a table using available binding for nodejs.
just for comparison, if i where using java on server the upload code would be like this:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
cors(response);
LOG.info("POST started: " + request.getContentType());
Collection<Part> parts = request.getParts();
for (Part part : parts) {
long len = part.getSize();
String name = part.getName();
LOG.info("attepmt to upload " + name + " from "
+ request.getRemoteAddr());
if ("file".equals(name)) {
String mime = part.getContentType();
try (InputStream in = part.getInputStream()) {
String q = "insert into media (mediamime,mediadata) values (?,?)";
try (Connection con = ds.getConnection()) {
try (PreparedStatement ps = //
con.prepareStatement(q, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, mime);
ps.setBinaryStream(2, in, len);
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()){
String m = ""+rs.getInt(1);
LOG.info("new media: "+m);
response.getWriter().write(m);
}
}
}
} catch (Exception e) {
LOG.severe(e.getMessage());
e.printStackTrace();
response.setStatus(500);
response.getWriter().write(e.toString());
}
}
}
}
}
Attention for ps.setBinaryStream(2, in, len); part.
Im' looking for a equivalent to this using pg, knex, anything capable of this in the nodejs ecossytem.
Thanks in advance.
On 10/26/2015 03:28 PM, Leonardo wrote: > Hello all, > > i'm looking for a way to insert a file into a table using available > binding for nodejs. > > just for comparison, if i where using java on server the upload code > would be like this: > > protected void doPost(HttpServletRequest request, > HttpServletResponse response) throws ServletException, IOException { > cors(response); > LOG.info("POST started: " + request.getContentType()); > Collection<Part> parts = request.getParts(); > for (Part part : parts) { > long len = part.getSize(); > String name = part.getName(); > LOG.info("attepmt to upload " + name + " from " > + request.getRemoteAddr()); > if ("file".equals(name)) { > String mime = part.getContentType(); > try (InputStream in = part.getInputStream()) { > String q = "insert into media (mediamime,mediadata) values (?,?)"; > try (Connection con = ds.getConnection()) { > try (PreparedStatement ps = // > con.prepareStatement(q, Statement.RETURN_GENERATED_KEYS)) { > ps.setString(1, mime); > ps.setBinaryStream(2, in, len); > ps.executeUpdate(); > try (ResultSet rs = ps.getGeneratedKeys()) { > if (rs.next()){ > String m = ""+rs.getInt(1); > LOG.info("new media: "+m); > response.getWriter().write(m); > } > } > } > } catch (Exception e) { > LOG.severe(e.getMessage()); > e.printStackTrace(); > response.setStatus(500); > response.getWriter().write(e.toString()); > } > } > } > } > } > > Attention for ps.setBinaryStream(2, in, len); part. > > Im' looking for a equivalent to this using pg, knex, anything capable of > this in the nodejs ecossytem. I do not use nodejs, but I did find this: http://stackoverflow.com/questions/13124711/storing-a-file-in-postgres-using-node-postgres > > Thanks in advance. -- Adrian Klaver adrian.klaver@aklaver.com
Hello Adrian,
i stumbled a few weeks ago into this, but this approach relies to the disk instead of stream. since i can't guarantee disk reliability (clustering, disk may not be the same), i can not use this approach.
thanks for the answer, i'll keep searching.
2015-10-27 10:37 GMT-03:00 Adrian Klaver <adrian.klaver@aklaver.com>:
I do not use nodejs, but I did find this:On 10/26/2015 03:28 PM, Leonardo wrote:Hello all,
i'm looking for a way to insert a file into a table using available
binding for nodejs.
just for comparison, if i where using java on server the upload code
would be like this:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
cors(response);
LOG.info("POST started: " + request.getContentType());
Collection<Part> parts = request.getParts();
for (Part part : parts) {
long len = part.getSize();
String name = part.getName();
LOG.info("attepmt to upload " + name + " from "
+ request.getRemoteAddr());
if ("file".equals(name)) {
String mime = part.getContentType();
try (InputStream in = part.getInputStream()) {
String q = "insert into media (mediamime,mediadata) values (?,?)";
try (Connection con = ds.getConnection()) {
try (PreparedStatement ps = //
con.prepareStatement(q, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, mime);
ps.setBinaryStream(2, in, len);
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()){
String m = ""+rs.getInt(1);
LOG.info("new media: "+m);
response.getWriter().write(m);
}
}
}
} catch (Exception e) {
LOG.severe(e.getMessage());
e.printStackTrace();
response.setStatus(500);
response.getWriter().write(e.toString());
}
}
}
}
}
Attention for ps.setBinaryStream(2, in, len); part.
Im' looking for a equivalent to this using pg, knex, anything capable of
this in the nodejs ecossytem.
http://stackoverflow.com/questions/13124711/storing-a-file-in-postgres-using-node-postgres
Thanks in advance.
--
Adrian Klaver
adrian.klaver@aklaver.com
On 10/28/2015 09:08 AM, Leonardo wrote: > Hello Adrian, > > i stumbled a few weeks ago into this, but this approach relies to the > disk instead of stream. since i can't guarantee disk reliability > (clustering, disk may not be the same), i can not use this approach. > > thanks for the answer, i'll keep searching. I got to believe Knex can handle this: http://knexjs.org/#Interfaces-Streams and it seems to 'know' binary: http://knexjs.org/#Schema-binary Possible solution, I just do not know enough nodejs to be sure: https://github.com/tgriesser/knex/issues/756 Otherwise you could probably ask there. > > 2015-10-27 10:37 GMT-03:00 Adrian Klaver <adrian.klaver@aklaver.com > <mailto:adrian.klaver@aklaver.com>>: > > On 10/26/2015 03:28 PM, Leonardo wrote: > > Hello all, > > i'm looking for a way to insert a file into a table using available > binding for nodejs. > > just for comparison, if i where using java on server the upload code > would be like this: > > protected void doPost(HttpServletRequest request, > HttpServletResponse response) throws ServletException, IOException { > cors(response); > LOG.info("POST started: " + request.getContentType()); > Collection<Part> parts = request.getParts(); > for (Part part : parts) { > long len = part.getSize(); > String name = part.getName(); > LOG.info("attepmt to upload " + name + " from " > + request.getRemoteAddr()); > if ("file".equals(name)) { > String mime = part.getContentType(); > try (InputStream in = part.getInputStream()) { > String q = "insert into media (mediamime,mediadata) values (?,?)"; > try (Connection con = ds.getConnection()) { > try (PreparedStatement ps = // > con.prepareStatement(q, Statement.RETURN_GENERATED_KEYS)) { > ps.setString(1, mime); > ps.setBinaryStream(2, in, len); > ps.executeUpdate(); > try (ResultSet rs = ps.getGeneratedKeys()) { > if (rs.next()){ > String m = ""+rs.getInt(1); > LOG.info("new media: "+m); > response.getWriter().write(m); > } > } > } > } catch (Exception e) { > LOG.severe(e.getMessage()); > e.printStackTrace(); > response.setStatus(500); > response.getWriter().write(e.toString()); > } > } > } > } > } > > Attention for ps.setBinaryStream(2, in, len); part. > > Im' looking for a equivalent to this using pg, knex, anything > capable of > this in the nodejs ecossytem. > > > I do not use nodejs, but I did find this: > > http://stackoverflow.com/questions/13124711/storing-a-file-in-postgres-using-node-postgres > > > Thanks in advance. > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com> > > -- Adrian Klaver adrian.klaver@aklaver.com