JavaScript uploads files (psd, compressed packages, etc.), images, video implementation methods, javascriptpsd

Source: Internet
Author: User

JavaScript uploads files (psd, compressed packages, etc.), images, video implementation methods, javascriptpsd

If you don't talk much about it, paste the Code directly. The specific code is as follows:

// Upload target trigger point <input type = "file" class = "upvideo" name = "upvideo" id = "fileupload1"/> // introduce the plug-in <script type = "text/ javascript "src =" {$ IMG}/bstage/js/jquery. form. js "language =" javascript "> </script>/** method call **/setFileUpload ({name: 'fileupload1', backfun: upback1, beforefun: checkUp1, loadfun: loadup, phpUrl: '/ossphp/php/get. php? Ft = video'}); // verify the function checkUp1 (n) before uploading {// view the video size (mb) var fileObj = document. getElementById (n ). files [0]; var relSize = parseInt (fileObj. size/1024/1024); if (relSize> 10) {// greater than 10 mb alert ('hprompt ',' the screen size exceeds 10 MB. please upload it again! '); Return false;} // view the video type var TV _id = document. getElementById (n ). value; // obtain the value var index = TV _id.indexOf (". "); TV _id = TV _id.substring (index ). toLowerCase (); if (TV _id! = ". Rb" & TV _id! = ". Rmvb" & TV _id! = ". Mp4" & TV _id! = ". Flv ") {alert (" prompt "," not specify video format, reselect "); return false;} return true;} // function loadup () in upload () {} // after successful upload, operate function upback1 (data) {// parameters after successful data}/** upload function encapsulation **/function setFileUpload (options) {var n = options. name; var phpUrl = options. phpUrl; var backfun = options. backfun; var beforefun = options. beforefun; var loadfun = options. loadfun; var accessid = ''; var accesskey =''; var host = ''; var PolicyBase64 = ''; var signature =''; var callbackbody = ''; var filename =''; var dirname = ''; var expire = 0; var upflag = true; if (phpUrl = null | phpUrl = '') {console. log ('phpurl is null'); return false;} if (n = null | n = '') {console. log ('object is null'); return false;} var backfun1 = $. isFunction (backfun )? Backfun: function (success, error) {console. log ('no backfun ') ;}; var beforefun1 = $. isFunction (beforefun )? Beforefun: function (success, error) {return true ;}; var loadfun1 =1. isFunction (loadfun )? Loadfun: function (success, error) {return true ;}; var obj =$ ('#' + n); obj. change (function () {if (beforefun1 (n) {if (n = "fileupload1") {fileEmb1 (n);} else if (n = 'fileupload2 ') {fileEmb2 (n);} else if (n = 'fileupload3') {fileEmb3 (n) ;}}); function fileEmb1 (n) {// oss add $ ("#" + n ). attr ('name', 'file'); var file1 = $ ("#" + n ). val (); var fileName = getFileName (file1); phpUrl = phpUrl + '& fname =' + file Name; get_signature (); var ldot = fileName. lastIndexOf (". "); var filetype = fileName. substring (ldot + 1); $ ("#" + n ). wrap ("<form id = 'myupload1' action = '" + host + "'method = 'post' enctype = 'multipart/form-data'> </form> "); $ ("# myupload1 "). prepend ("<input type = 'ddy' value ='' name = 'name' id = 'name'> </input> <input type = 'ddy' value = '" + dirname + ". "+ filetype +" 'name = 'key'> </input> <input type = 'den den 'value = '"+ PolicyBase64 +" 'name = 'policy' id = 'policy'> </input> <input type = 'den den' value = '"+ accessid +" 'name = 'ossaccesskeyid' id = 'ossaccesskeyid'> </input> <input type = 'did' value = '000000' name = 'success _ action_status 'id = 'success _ action_status'> </input> <input type = 'den den 'value =' "+ callbackbody +" 'name = 'callback' id = 'callback'> </input> <input type = 'did' value = '"+ signature +" 'name = 'signature' id = 'signatur E '> </input> "); // speed_width progress bar class // percent progress number class // load_content upload area $ (' # myupload1 '). ajaxSubmit ({dataType: 'json', // the data format is json data: '', beforeSend: function () {loadfun1 (); if (! Upflag) return false; upflag = false;}, uploadProgress: function (event, position, total, percentComplete) {$ ('. upck_ B '). hide (); $ ('. videspeed '). show (); percentComplete = percentComplete & gt; = 100? 99: percentComplete; var percentVal = percentComplete + '%'; // obtain the progress ('.speed_num'0000.css ('width', percentVal); percentile (percentComplete) ;}, success: function (data) {// success response ('.load_content'{.html ('<em> Image Upload </em>'); // restore the image after the upload is successful $ ("#" + n ). attr ('name', ''); setFileUpload (options); upflag = true; backfun1 (data) ;}, error: function (xhr) {// upload failure ('.load_content'{.html ('<em> Image Upload </em>'); upflag = true; });} Function getFileName (path) {var pos1 = path. lastIndexOf ('/'); var pos2 = path. lastIndexOf ('\'); var pos = Math. max (pos1, pos2) if (pos <0) return path; else return path. substring (pos + 1);} now = timestamp = Date. parse (new Date ()/1000; new_multipart_params = ''; function send_request () {var xmlhttp = null; if (window. XMLHttpRequest) {xmlhttp = new XMLHttpRequest ();} else if (window. acti VeXObject) {xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP");} if (xmlhttp! = Null & phpUrl) {// phpUrl = '/ossphp/php/get. php? Ft = resources '; xmlhttp. open ("GET", phpUrl, false); xmlhttp. send (null); return xmlhttp. responseText;} else {alert ("Your browser does not support XMLHTTP. ") ;}}; function get_signature () {// you can determine whether expire has exceeded the current time. If expire has exceeded the current time, retrieve it again. 3 s as the buffer now = timestamp = Date. parse (new Date ()/1000; // if (expire <now + 3) // {body = send_request () var obj = eval ("(" + body + ")"); host = obj ['host'] policyBase64 = obj ['policy'] accessid = obj ['accesssid '] signature = obj ['signature'] expire = parseInt (obj ['expire ']) callbackbody = obj ['callback'] dirname = obj ['dir'] return true ;//}};}

Note: The details can be further encapsulated.

The above section describes how to upload files (psd, compressed packages, etc.), images, and video functions in JavaScript. I hope this will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.