Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

86 rindas
2.2 KiB

  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 $upload = $("<div class='file-upload'>" + repl(wn._('Upload a file')+':<br>\
  7. <input type="file" name="filedata" /><br><br>\
  8. OR:<br><input 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(_("Please attach a file or set a URL"));
  44. return;
  45. }
  46. var _upload_file = function() {
  47. var msgbox = msgprint(wn._("Uploading..."));
  48. 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. onerror(r);
  56. return;
  57. }
  58. callback(r.message.fid, r.message.filename, r);
  59. $(document).trigger("upload_complete",
  60. [r.message.fid, r.message.filename]);
  61. }
  62. });
  63. }
  64. if(args.file_url) {
  65. _upload_file();
  66. } else {
  67. var freader = new FileReader();
  68. freader.onload = function() {
  69. args.filename = fileobj.name;
  70. args.filedata = freader.result.split(",")[1];
  71. _upload_file();
  72. };
  73. freader.readAsDataURL(fileobj);
  74. }
  75. }
  76. }