Mongodb/GridFS - Am using reactjs for file upload/download having issue -
am trying upload files , getting error "unable process parts no multi-part configuration has been provided". using reactjs , servlet connect mongodb. please find code below.
upload(data) { ajax({ url: 'api/uploaddocservlet', method: 'post', data: data }, (data) => { console.log("print data::::"+data); if (data.error == null) { this.search({}); } else { alert(data.error); } }); }
api servlet code :
try { system.out.println("uploaddocservlet:::"+request.getpart("data")); (part part : request.getparts()) { string filename = extractfilename(part); object result = service.uploaddoc(filename); response.put("result", result != null ? result : jsonobject.null); } } catch (exception e) { logger.log(level.warning, null, e); response.put("error", e.getmessage()); } resp.setcontenttype("application/json"); resp.setcharacterencoding("utf-8"); resp.adddateheader("expires", 0); printwriter pw = resp.getwriter(); response.write(pw); pw.close(); } private string extractfilename(part part) { string contentdisp = part.getheader("content-disposition"); string[] items = contentdisp.split(";"); (string s : items) { if (s.trim().startswith("filename")) { return s.substring(s.indexof("=") + 2, s.length()-1); } } return ""; }
file insert in mongodb (java code )
public void uploaddocument(string file) { string filename = file; filename = new file(filename).getname(); gridfs gfsphoto = new gridfs((db) database, "photo"); gridfsinputfile gfsfile = gfsphoto.createfile(filename); gfsfile.setfilename(filename); gfsfile.save(); system.out.println("saved..."); }
could suggest on same.
Comments
Post a Comment