You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload.js 1.9 KiB

12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2013 Web Notes Technologies Pvt Ltd
  2. // License: MIT. See license.txt
  3. // parent, args, callback
  4. wn.upload = {
  5. make: function(opts) {
  6. var id = wn.dom.set_unique_id();
  7. $(opts.parent).append();
  8. var $upload = $("<div class='file-upload'>" + repl(wn._('Upload a file')+':<br>\
  9. <input type="file" name="filedata" /><br><br>\
  10. OR:<br><input type="text" name="file_url" /><br>\
  11. <p class="help">'
  12. + (opts.sample_url || 'e.g. http://example.com/somefile.png')
  13. + '</p><br>\
  14. <input type="submit" class="btn btn-info btn-upload" value="'
  15. +wn._('Attach')+'" /></div>', {
  16. id: id,
  17. action: wn.request.url
  18. })).appendTo(opts.parent)
  19. // get the first file
  20. $upload.find(".btn-upload").click(function() {
  21. // convert functions to values
  22. for(key in opts.args) {
  23. if(typeof val==="function")
  24. opt.args[key] = opts.args[key]();
  25. }
  26. // add other inputs in the div as arguments
  27. $upload.find("input[name]").each(function() {
  28. var key = $(this).attr("name");
  29. if(key!="filedata" && key!="file_url") {
  30. opts.args[key] = $(this).val();
  31. }
  32. })
  33. opts.args.file_url = $upload.find('[name="file_url"]').val();
  34. var fileobj = $upload.find(":file").get(0).files[0];
  35. wn.upload.upload_file(fileobj, opts.args, opts.callback);
  36. })
  37. },
  38. upload_file: function(fileobj, args, callback) {
  39. if(!fileobj && !args.file_url) {
  40. msgprint(_("Please attach a file or set a URL"));
  41. return;
  42. }
  43. var freader = new FileReader();
  44. freader.onload = function() {
  45. args.filedata = freader.result.split(",")[1];
  46. args.filename = fileobj.name;
  47. var msgbox = msgprint(wn._("Uploading..."));
  48. wn.call({
  49. "method": "uploadfile",
  50. args: args,
  51. callback: function(r) {
  52. msgbox.hide();
  53. if(r.exc) {
  54. msgprint("There were errors in uploading.");
  55. }
  56. callback(r.message, fileobj.name);
  57. }
  58. });
  59. }
  60. freader.readAsDataURL(fileobj);
  61. }
  62. }