window.filegatewayUrl = "/filegw/" function initFile() { window.fileShareName = "drive_c$"; $('option').val(window.fileShareName).appendTo('#shares'); window.currentPath = []; updatePathText(); listFile(); setInterval(listFile, 20000); } function getCurrentPath() { return window.filegatewayUrl + window.fileSessionId + "/" + encodeURI(window.fileShareName) + "/" + encodeURI(window.currentPath.join("/")) + "/" } function getSize(fileSize) { var size = parseFloat(fileSize); var units = ['B', 'KB', 'MB', 'GB', 'TB']; var i = 0; while (size >= 1024 && i < units.length - 1) { size /= 1024; i++; } return size.toFixed(2) + ' ' + units[i]; } function listFile() { $.ajax({ url: getCurrentPath(), method: "GET", success: function (response) { var data = response; var table = document.getElementById("filesharelist"); table.innerHTML = ""; if (window.currentPath.length > 0) { var row = table.insertRow(); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); cell1.innerHTML = "↑🔝🔝🔝↑"; cell2.innerHTML = "向上"; } for (var i = 0; i < data.entries.length; i++) { if (data.entries[i].fileAttributes.indexOf("Hidden") != -1) continue; var row = table.insertRow(); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); var filename = data.entries[i].filename.slice(2); if (data.entries[i].type == "Directory") { cell1.innerHTML = "" + filename + ""; cell2.innerHTML = "目录"; } else { cell1.innerHTML = "" + filename + ""; cell2.innerHTML = getSize(data.entries[i].fileSize); } } }, error: function (xhr, status, error) { $.alert("文件夹列出失败,可能没有权限或者凭证过期,刷新网页后再试"); } }); } function uploadFile(force) { var fileInput = document.getElementById('fileupload'); var file = fileInput.files[0]; var suffix = ""; var blob = new Blob([file], { type: "application/octet-stream" }); $.ajax({ url: getCurrentPath() + encodeURI(file.name) + suffix, method: "POST", data: blob, contentType: false, processData: false, success: function (response) { console.log("upload response", response) if (response.exists && !force) { $.confirm({ title: '文件已存在!', content: '是否替换?', buttons: { replace: { text: '替换', btnClass: 'btn-red', keys: ['enter', 'shift'], action: function () { uploadFile(true) } }, cancel: { text: '取消', action: function () {} } } }); } else if (!response.exists && !response.error) $.alert("上传成功"); }, error: function (xhr, status, error) { $.alert("上传失败,可能没有权限访问。") console.error("upload error", xhr, status, error) } }); } function mkdir() { $.confirm({ title: "请输入文件夹名称", content: '' + '
', buttons: { formSubmit: { text: '确定', btnClass: 'btn-blue', action: function () { var dirName = this.$content.find('.name').val(); $.ajax({ url: getCurrentPath() + encodeURI(dirName), method: "PUT", success: function (response) { if (!response.error) $.alert("创建成功"); }, error: function (xhr, status, error) { $.alert("创建文件夹失败,可能是没有权限"); } }); } }, cancel: { text: '取消', action: function() {} }, }, onContentReady: function () { // bind to events var jc = this; this.$content.find('form').on('submit', function (e) { // if the user submits the form by pressing enter in the field. e.preventDefault(); jc.$$formSubmit.trigger('click'); // reference the button and click it }); } }); } function onClickOpenDir(element) { window.currentPath = window.currentPath.concat(element.innerHTML); updatePathText(); listFile(); } function updatePathText() { $("#currentPath").text("/"+window.fileShareName+"/"+window.currentPath.join("/")+"/") }