Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

82 строки
2.1 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);
  39. })
  40. },
  41. upload_file: function(fileobj, args, callback) {
  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. msgbox.hide();
  53. if(r.exc) {
  54. msgprint("There were errors in uploading.");
  55. }
  56. callback(r.message, args.filename || args.file_url, r);
  57. }
  58. });
  59. }
  60. if(args.file_url) {
  61. _upload_file();
  62. } else {
  63. var freader = new FileReader();
  64. freader.onload = function() {
  65. args.filename = fileobj.name;
  66. args.filedata = freader.result.split(",")[1];
  67. _upload_file();
  68. };
  69. freader.readAsDataURL(fileobj);
  70. }
  71. }
  72. }