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 2.5 KiB

12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. // parent, args, callback
  4. wn.upload = {
  5. make: function(opts) {
  6. var $upload = $("<div class='file-upload'>" + repl(wn._('Upload a file')+':<br>\
  7. <input class="alert alert-info" style="padding: 7px; margin-top: 7px;" type="file" name="filedata" />\
  8. OR:<br><input class="form-control" style="max-width: 300px;" type="text" name="file_url" /><br>\
  9. <p class="help">'
  10. + (opts.sample_url || 'e.g. http://example.com/somefile.png')
  11. + '</p><br>\
  12. <input type="submit" class="btn btn-info btn-upload" value="'
  13. +wn._('Attach')+'" /></div>', {
  14. action: wn.request.url
  15. })).appendTo(opts.parent);
  16. // get the first file
  17. $upload.find(".btn-upload").click(function() {
  18. // convert functions to values
  19. for(key in opts.args) {
  20. if(typeof val==="function")
  21. opt.args[key] = opts.args[key]();
  22. }
  23. // add other inputs in the div as arguments
  24. opts.args.params = {};
  25. $upload.find("input[name]").each(function() {
  26. var key = $(this).attr("name");
  27. var type = $(this).attr("type");
  28. if(key!="filedata" && key!="file_url") {
  29. if(type === "checkbox") {
  30. opts.args.params[key] = $(this).is(":checked");
  31. } else {
  32. opts.args.params[key] = $(this).val();
  33. }
  34. }
  35. })
  36. opts.args.file_url = $upload.find('[name="file_url"]').val();
  37. var fileobj = $upload.find(":file").get(0).files[0];
  38. wn.upload.upload_file(fileobj, opts.args, opts.callback, opts.onerror);
  39. })
  40. },
  41. upload_file: function(fileobj, args, callback, onerror) {
  42. if(!fileobj && !args.file_url) {
  43. msgprint(wn._("Please attach a file or set a URL"));
  44. return;
  45. }
  46. var _upload_file = function() {
  47. var msgbox = msgprint(wn._("Uploading..."));
  48. return wn.call({
  49. "method": "uploadfile",
  50. args: args,
  51. callback: function(r) {
  52. if(!r._server_messages)
  53. msgbox.hide();
  54. if(r.exc) {
  55. // if no onerror, assume callback will handle errors
  56. onerror ? onerror(r) : callback(null, null, r);
  57. return;
  58. }
  59. callback(r.message.fid, r.message.filename, r);
  60. $(document).trigger("upload_complete",
  61. [r.message.fid, r.message.filename]);
  62. }
  63. });
  64. }
  65. if(args.file_url) {
  66. _upload_file();
  67. } else {
  68. var freader = new FileReader();
  69. freader.onload = function() {
  70. args.filename = fileobj.name;
  71. args.filedata = freader.result.split(",")[1];
  72. _upload_file();
  73. };
  74. freader.readAsDataURL(fileobj);
  75. }
  76. }
  77. }