gpt4 book ai didi

jquery - 文件上传大小验证在 IE 上不起作用

转载 作者:行者123 更新时间:2023-12-01 05:54:04 26 4
gpt4 key购买 nike

您好,我使用 jQuery 实现了上传最大文件大小验证,它在
中运行良好 chrome 和 firefox 无法在 IE 中运行

$('#file').change(function() {           
fileSizeError = (this.files[0].size/(1024*1024) > 1) ? true : false;
});

请帮我解决这个问题

谷歌搜索后我找到了一个解决方案,那就是创建一个ActiveXObject。代码如下

var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");

但是我无法创建 ActiveXObject 我的目标是这段代码可以在 IE 上运行 其他浏览器也是如此。

最佳答案

**You can check this file size in server side using jquery webmethod**

正如 hoodedperson70 here 所说:

Sure thing, here's an example, but I cannot test...it should work though:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#submit1").click(function () {
var file == $("#file1");
if (file.value = "" || file.value == null)
{
alert("You must provide a file.");
}
else
{
ajaxFileUpload();
}
});
});

function ajaxFileUpload()
{
//starting setting some animation when the ajax starts and completes
$("#loading").ajaxStart(function () {
$(this).show();
})
.ajaxComplete(function () {
$(this).hide();
});

/*
preparing ajax file upload
url: the url of script file handling the uploaded files
fileElementId: the file type of input element id
dataType: it supports json, xml
secureuri: use secure protocol
success: call back function when the ajax complete
error: callback function when the ajax failed
*/
$.ajaxFileUpload
(
{
url: 'THE WEB SERVICE URL',
secureuri: false,
fileElementId: 'file1',
dataType: 'json',
success: function (data, status)
{
if (data.success == "true")
{
$("#results").html("Valid file size, " + data.length);
}
else
{
$("#results").html("Invalid file size, " + data.length);
}
},
error: function (data, status, e)
{
alert(data + ", " + e);
}
}
)

return false;
}
</script>
</head>
<body>
<form id="form1" method="POST" action="" enctype="multipart/form-data">
<input type="file" id="file1" name="file1" />
<input type="button" id="submit1" name="submit1" value="Upload (size will be checked)" />
<img src="loading.gif" alt="Loading" id="loading" style="display:none;" />
<br /><br /><br />
<div id="results"></div>
</form>
</body>
</html>

And here's the web service code:

[WebMethod]
public string CheckFileSize(HttpContext context)
{
var file1 = context.Request.Files[0];

if (file1 != null && file1.ContentLength > 0)
{
if (file1.ContentLength > 1024) // This is the IMPORTANT comparison
{
return "{\"success\":\"true\",\"length\":\"" + file1.ContentLength + "\"}";
}
else
{
return "{\"success\":\"false\",\"length\":\"" + file1.ContentLength + "\"}";
}
}
}

You will need to follow this link: http://www.phpletter.com/download_project_version.php?version_id=34 to download the .js file and the "loading.gif" file. For my example, they are in the same directory, but you may not have that, so you'll need to change their paths if necessary.

I hadn't realized they use PHP as the processing, but this should still work just the same. The javascript should be able to submit the file to any type of technology, so it depends on the web service logic. It's very possible that the use of HttpContext isn't right for this scenario, but you can test this out.

不仅仅是客户端验证的方式

否则,只需尝试这种方式

if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
//Display a warning
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file here


}

关于jquery - 文件上传大小验证在 IE 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com