我有一个代码来文件上传...在onchange事件检查文件大小,格式上传到物理文件夹之前。如果所有验证都为真,那么将从onchange事件触发上传按钮。
一切正常但我上传错误的格式文件并且会弹出验证错误并在下一个文件上传Page.Isvalid为false,因此上传的文件甚至无法上传正确的格式/大小。如何解决这个问题。
下面是代码:
的javascript
function UploadFile(fileUpload) {
if (fileUpload.value != '') {
var title = document.getElementById("<%=txtTitle.ClientID%>").value;
var sFileName = fileUpload.value.toLowerCase();
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1];
var file = (fileUpload.files[0].size / 1024);
var limit = <%= ConfigurationManager.AppSettings["MaxLimit"]%>;
var maxlimit = limit * 1024;
var type = document.getElementById("<%=hdnType.ClientID%>").value;
if (title != "") {
if (file > maxlimit) {
fileUpload.value = null;
ShowAlert("Sorry the file you are trying to upload exceeds the allowed file limit. Allowed size is 10MB.");
}
else {
if (type == 1) {
if (sFileExtension != "png" && sFileExtension != "jpg" && sFileExtension != "jpeg") {
fileUpload.value = null;
ShowAlert("File Format not supported. Please upload .jpeg/.png files");
}
else {
document.getElementById("<%=btnSave.ClientID%>").click();
}
}
}
}
<asp:FileUpload ID="upldfile" runat="server" onchange="UploadFile(this)" ClientIDMode="Static" />
<asp:Button ID="btnSave" runat="server" UseSubmitBehavior="false" ValidationGroup="save" Text="Save" style="display:none;" OnClick="Upload" data-dismiss="modal" class="btn btn-danger" />
上传活动
public void Upload(object sender, EventArgs e)
{
if (Page.IsValid)
{
SavePortfolio();
}
}
答案 0 :(得分:0)
试试这个
function UploadFile(fileUpload)
{
if (fileUpload.value != '') {
var title = document.getElementById("<%=txtTitle.ClientID%>").value;
var sFileName = fileUpload.value.toLowerCase();
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1];
var file = (fileUpload.files[0].size / 1024);
var limit = <%= ConfigurationManager.AppSettings["MaxLimit"]%>;
var maxlimit = limit * 1024;
var type = document.getElementById("<%=hdnType.ClientID%>").value;
if (title != "") {
if (file > maxlimit) {
fileUpload.value = null;
ShowAlert("Sorry the file you are trying to upload exceeds the allowed file limit. Allowed size is 10MB.");
return;
}
else {
if (type == 1) {
if (sFileExtension != "png" && sFileExtension != "jpg" && sFileExtension != "jpeg") {
fileUpload.value = null;
ShowAlert("File Format not supported. Please upload .jpeg/.png files");
return;
}
else {
document.getElementById("<%=btnSave.ClientID%>").click();
}
}
}
}
}
}