Uplaod图像上使用asp文件上传4 MB?文件上传、图像、Uplaod、MB

2023-09-10 18:31:03 作者:熹酌

我使用的是asp文件上传由我上传按钮上的图像点击。每当它崩溃。如果我在配置了maxRequestLength的定义大小,我上传图片超过4 MB 然后,如果我超过文件大小重新它crashes.Is能够限制用户,并显示一个声明消息,如果一个巨大的文件被上载。我的内涵是让用户知道该文件是巨大的。

I am using a asp file uploader by which i am uploading an image on a button click .whenever i am uploading an image over 4 mb it crashes .if i define a size in config for maxrequestlength then if i exceed the file size again it crashes.Is it possible to restrict user and show a disclaimer message if a huge file is uploaded . My intension is to let the user know that file is huge .

aspx页面

<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>
                        &nbsp;
                        <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页面事件。  保护无效btnUpload_Click(对象发件人,EventArgs的)         {             尝试             {                 //hfCurrentId.Value =工作项ID:+ hfWorkItemID.Value;                 如果(fileUploader.HasFile)                 {                     如果(fileUploader.PostedFile.ContentType.Contains(图像)及和放大器;                         fileUploader.PostedFile.ContentLength&LT; = 3145728)                     {

.cs page events. 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限制大小 和处理它的客户端大小

Here i am limiting the size by 3145728 and handling it in client size

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;
        }

有没有其他的方式,以避免

is there any other way to avoid

var objFSO = new ActiveXObject("Scripting.FileSystemObject");

我不能使用的ActiveXObject根据一些客户的问题

I can't use ActiveXObject as per some client issue

推荐答案

假如你不使用HTML5。假如你不使用所建议的卡尔定制/第三方组件。

Assuming that you are not using HTML5. Assuming you are not using custom/third-party components as suggested by Karl.

(1)您将需要首先限制上传大小在web.config中的IIS:

(1) You will need to first limit the upload size in web.config for IIS:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="YOUR_MAX_ALLOWED_SIZE IN BYTES"/>
        </requestFiltering>
    </security>
</system.webServer>

(2)确保值匹配的httpRuntime的ASP.Net:

(2) Make sure the values are matching in httpRuntime for ASP.Net:

<httpRuntime executionTimeout="SECONDS" maxRequestLength="YOUR_MAX_ALLOWED_SIZE IN KB"/>

(3)检查大小超过服务器端code和适当的告知用户:

(3) Check if the size exceeds in your server-side code and suitably inform the user:

if (fileUpload.PostedFile.ContentLength > (YOUR_MAX_ALLOWED_SIZE - n)) {
    // where "n" is arbitrary number to intercept uploads before IIS kicks it
    // you display limit to users as (YOUR_MAX_ALLOWED_SIZE - n)
    // inform problem to the user here
}

您不能,除非你使用HTML5文件API在客户端进行检查。因此,用户必须等待,直到整个文件被上载,然后将其刷爆。因此,精心设置YOUR_MAX_ALLOWED_SIZE。

You cannot check it at the client-side unless you are using HTML5 File API. So, the user has to wait until the entire file is uploaded and then it is maxed out. Hence, set your YOUR_MAX_ALLOWED_SIZE carefully.

更新

正如你看到的,这是困难的,如果在大小超过限制来抓。有几个解决方法:

As you see, it is difficult to catch if the size exceeds the limit. There a few workarounds:

设置自定义错误:

Set up custom errors:

   

system.webServer 设置 httpErrors 部分:

请参阅由Marcus在这里接受的答案:Display当文件上传超出允许大小的ASP.NET MVC 自定义错误页

See the accepted answer by Marcus here: Display custom error page when file upload exceeds allowed size in ASP.NET MVC