百度個人云盤空間大,完全免費,而且提供了pcs api供調用操作文件,在平時的項目里往里面保存一些文件是很實用的。
環境準備:
開通讀寫網盤的權限及獲取access_token:http://blog.csdn.net/langyuezhang/article/details/47206621
百度官方pcs api文檔:http://developer.baidu.com/wiki/index.php?title=docs/pcs/overview,上面有各種語言的SDK,我用的laravel5,直接導里面的php SDK還用不了,稍做了修改才能用的。
看完上面兩篇文章,基本就可以使用了,下面貼出寫直接把用戶選擇的圖片預覽然后upload到網盤,上傳的api:
https://pcs.baidu.com/rest/2.0/pcs/file?method=upload&path=%2fapps%2wp2pcs%2f1.JPG&access_token=***紅色部分是固定的,因為只允許上傳到這個目錄下,實際對應到網盤的目錄是我的網盤/我的應用數據/wp2pcs、,無需先創建目錄再上傳文件,只需要指定路徑,它會自動創建,如你想把圖片上傳到/apps/wp2pcs/img下,path就寫/apps/wp2pcs/img/1.jpg就可以了。下面是把用戶在網頁上選擇的圖片,預覽后直接上傳到網盤的代碼,參照了網上的例子:預覽:
//圖片上傳預覽 IE是用了濾鏡。function previewImage(file, product){getPhotopty();console.log("previewImage");uploadAndSubmit(product);var div = document.getElementById('preview' + product);var fileName = file.value;//upload();if (file.files){var i = 0;var funAppendImage = function () {var _file = file.files[i];if (_file) {var reader = new FileReader()reader.onload = function (evt) {fileName = _file.name;div.innerHTML += '<div class="col-xs-6 col-md-3"><a style="float:right;cursor:pointer;" onclick="del(this)">X</a><div class="thumbnail"><img id=imghead' + product + fileName + '></div></div>';var img = document.getElementById('imghead' + product + fileName);img.src = evt.target.result;i++;funAppendImage();}reader.readAsDataURL(_file);}};funAppendImage();}$('#coverBg').show();$('#coverDiv').show();//$("#uploadFrm" + product).submit();}
上傳:
var access_token = "***********";var baseUrl = "https://c.pcs.baidu.com/rest/2.0/pcs/";function uploadAndSubmit(product) {console.log("start uploadAndSubmit");if (typeof FileReader == 'undefined') {alert("你的瀏覽器不支持FileReader接口!");}var taskName = $("#txtTask").val() + "-" + $("#txtTask2").val();var form = document.forms["uploadFrm" + product];console.log("form:" + form);var fileCtrl = "filectrl" + product;console.log("filectrl:" + fileCtrl);//if (form[fileCtrl].files.length > 0)console.log($("#filectrl" + product)[0]);if ($("#filectrl" + product)[0].files.length > 0){for (var i = 0; i < $("#filectrl" + product)[0].files.length; i++){var file = form[fileCtrl].files[i];console.log(file.name);var filePath = "%2fapps%2fwp2pcs%2f" + taskName + "%2f" + file.name;console.log("add exif info to db");getExifIinfo(taskName, file, product, filePath);//document.getElementById("bytesRead").textContent = file.size;console.log("start XMLHttpRequest");var xhr = new XMLHttpRequest();console.log(access_token);var url = baseUrl + "file?method=upload&path=%2fapps%2fwp2pcs%2f" + taskName + "%2f" + file.name + "&access_token=" + access_token + "&ondup=overwrite&count=" + i;console.log(url);xhr.open("POST", url, true);var formData = new FormData();formData.append("file", file);console.log("onreadystatechange");xhr.onreadystatechange = function () {console.log("onreadystatechange start");//console.log(xhr.status);if (xhr.readyState == 4) {if (xhr.status == 200) {console.log("upload complete");console.log("response: " + xhr.responseText);var result = $.parseJSON(xhr.responseText);if (result.hasOwnProperty("path")){$("#reusltMsg").append('<div class="alert alert-success" role="alert"> 上傳成功.</div>');} else{$("#reusltMsg").append('<div class="alert alert-danger" role="alert"> 上傳失敗.</div>');}} else{$("#reusltMsg").append('<div class="alert alert-danger" role="alert"> 上傳失敗(200).</div>');}}$('#coverBg').hide();$('#coverDiv').hide();}xhr.send(formData);}} else{alert("Please choose a file.");$('#coverBg').hide();$('#coverDiv').hide();}}
新聞熱點
疑難解答