我正在使用一个asp文件上传器,通过它我在单击按钮上上传图像。每当我上传超过4 mb的图像时,它就会崩溃。如果我在配置中为maxrequestlength定义了一个大小,那么如果我再次超过文件大小它就会崩溃.如果上传大文件,是否可以限制用户并显示免责声明消息。我的意图是让用户知道文件很大。
页面
<tr>
<td align="center" class="title" colspan="2">
Image Upload
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblImageDisclaimer" runat="server" Text="Please upload an image file less than 3MB in size"
Font-Bold="true" ForeColor="Highlight"></asp:Label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:DropDownList ID="ddlImageType" runat="server" AutoPostBack="false" OnSelectedIndexChanged="ddlImageType_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan="2" id="tdUpload" runat="server">
<asp:Label ID="lblImage1" runat="server" Text="Upload images : " Font-Bold="true"></asp:Label>
<asp:FileUpload ID="fileUploader" runat="server" /><asp:Button ID="btnUpload" CssClass="button"
runat="server" Text="Upload" OnClick="btnUpload_Click" OnClientClick="javascript:return validateFile();" />
<asp:Label ID="lblMessage" runat="server" Text="" CssClass="errorText"></asp:Label>
</td>
</tr>
<tr>
<td id="Td2" runat="server" align="center" colspan="1">
<asp:Label ID="Label1" runat="server" Text="SCREENSHOT 1" Font-Bold="true"></asp:Label>
</td>
<td id="Td3" runat="server" align="center" colspan="1">
<asp:Label ID="Label2" runat="server" Text="SCREENSHOT 2" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td id="imgHolder" runat="server" align="center" colspan="1">
<div style="border: SOLID 1px BLACK; width: 575px; height: 250px; overflow: auto;">
<%--<asp:Image ID="imgPreview" runat="server" ImageUrl="~/FIRE/ImageHandler.ashx?id=1&imageNo=2" />--%>
<asp:Image ID="imgPreview" runat="server" />
</div>
</td>
<td id="imgHolder2" runat="server" align="center" colspan="1">
<div style="border: SOLID 1px BLACK; width: 575px; height: 250px; overflow: auto;">
<%--<asp:Image ID="imgPreview" runat="server" ImageUrl="~/FIRE/ImageHandler.ashx?id=1&imageNo=2" />--%>
<asp:Image ID="imgPreview2" runat="server" />
</div>
</td>
</tr>
<tr>
<td id="tdDelete1" align="left" style="width: 100%; padding-left: 15px;" colspan="1">
<asp:Button ID="btnDelete1" runat="server" Text="Delete" CssClass="button" OnClick="btnDelete_Click"
ToolTip="Click to Delete" OnClientClick="javascript:return confirm('Are you sure you want to delete this image?');" />
</td>
<td id="tdDelete2" align="left" style="width: 100%" colspan="1">
<asp:Button ID="btnDelete2" runat="server" Text="Delete" CssClass="button" OnClick="btnDelete_Click"
ToolTip="Click to Delete" OnClientClick="javascript:return confirm('Are you sure you want to delete this image?');" />
</td>
</tr>
.cs 页面事件。protected void btnUpload_Click(object sender, EventArgs e) { try { //hfCurrentId.Value = "Work item id:" + hfWorkItemID.Value; if (fileUploader.HasFile) { if (fileUploader.PostedFile.ContentType.Contains("image") && fileUploader.PostedFile.ContentLength <= 3145728) {
if (ddlImageType.SelectedValue == "1")
{
Session["FileBytes"] = fileUploader.FileBytes;
hfImage1Byte.Value = Convert.ToBase64String(fileUploader.FileBytes);
imgPreview.ImageUrl = "~/Handlers/ImageHandler.ashx?workItemId=" + workItemId
+ "&imageTypeId=" + ddlImageType.SelectedValue;
}
else
{
Session["FileBytes2"] = fileUploader.FileBytes;
hfImage2Byte.Value = Convert.ToBase64String(fileUploader.FileBytes);
imgPreview2.ImageUrl = "~/Handlers/ImageHandler.ashx?workItemId=" + workItemId
+ "&imageTypeId=" + ddlImageType.SelectedValue;
}
}
else
{
lblMessage.Text = Constants.ImageFileError;
}
}
else
{
lblMessage.Text = Constants.FileError;
}
//lblHeader.Text = hfCurrentId.Value;
//upPanel1.Update();
}
catch (System.Exception ex)
{
//DisplayError(ex);
Session["ExceptionDetails"] = ex;
Response.Redirect(Constants.FIREErrorDetails);
}
}
在这里,我将大小限制为 3145728 并以客户端大小处理
function validateFile() {
var strFileName = document.getElementById('<%=fileUploader.ClientID %>').value;
var strExtName = strFileName.substring(strFileName.lastIndexOf('.')).toLowerCase();
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var e = objFSO.getFile(strFileName);
var fileSize = e.size;
//file size limit for 3mb
if (fileSize > 3145728) {
alert("Maximum size of uploaded file should be less than 3 MB.");
return false;
}
else
return true;
}
有没有其他方法可以避免
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
根据某些客户端问题,我无法使用 ActiveXObject