我手上的脚本是我希望用来在我的应用程序中上传文件的。我无法真正使用任何现有的控件,因为我需要在上传文件之前对路径和内容进行一些特定的更改。
我有以下(不完整)代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="../plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="../plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<form enctype="multipart/form-data">
<span class="btn btn-file btn-primary" id="media-upload-btn">
<span class="fileupload-new" title="Accepted formats (.gif, .jpeg, .png)">Choose image(s)</span>
<span class="fileupload-exists">Choose another image</span>
<input id="user-file-to-upload" type="file" name="file" accept="image/gif, image/jpeg, image/png"/>
</span><br /><br />
<input type="button" onclick="upload();" value="Upload" />
</form>
</div>
</form>
<script type="text/javascript">
function upload() {
alert("test");
// var acceptedFileSize = $('#upload-image-size').val();
var acceptedFileSize = 10000;
alert(acceptedFileSize);
if (window.FormData !== undefined) {
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append("file" + i, files[i]);
var fileSize = files[i].size;
sp.base.log('fileSize(b): ' + fileSize);
sp.base.log('acceptedFileSize(kb): ' + acceptedFileSize);
if (parseInt(fileSize) > (parseInt(acceptedFileSize) * 1024)) {
$('#btnUpload').button('reset');
sp.base.show('The file is too large!<br>Cant be larger than ' + (parseInt(acceptedFileSize) / 1024) + ' MB. ' + ' and the current file size is ' + Math.round((parseInt(fileSize) / 1024 / 1024)) + ' MB.', 'error', 12000);
return false;
}
}
var url = "upload2.aspx";
sp.base.call(
'POST',
url,
data,
function (returnData) {
alert("Works");
onSuccessUploadImage(returnData, callback);
},
null,
function () {
// onError
alert("Error");
$('#btnUpload').button('reset');
},
null,
true,
false
);
}
}
</script>
目前,我不确定如何将文件实际收集到文件对象中,我还希望能够添加多个文件,而不仅仅是一个。