在asp.net 3.5中使用fileupload无法上传大于4 mb的文件

时间:2012-04-24 07:29:57

标签: c# asp.net file-upload smtpclient

我必须发送带附件的邮件。我的代码仅适用于小于4mb的文件。 我已经检查了网上的所有内容,但每个人都建议使用相同的解决方案。这就是在webconfig中更改我已经完成的httpruntime属性。

<httpRuntime maxRequestLength="10000" executionTimeout="1500"  />

我已经更改了web config中具有“timeout”属性的所有内容。还在IIS中的应用程序配置中对KeepAlive进行了更改,但即使在完成所有这些更改后问题仍然存在于我的应用程序中。每次我尝试上传更大的文件正好1.5分钟后连接超时超过4mb。

点击事件中的代码

protected void btnSend_Click(object sender, EventArgs e)
        {
            MailMessage msg = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            string strFrom = txtFrom.Text;
            string strTo = txtTo.Text;
            string strSubject= ddlTemplate.SelectedItem.Text.ToString();
            string strBody =txtBody.Text;
            string strCC =txtCC.Text;
            string strBCC =txtBCC.Text;
            if (this.fuAttachments.HasFile)
            {
                Attachment at = new Attachment(fuAttachments.PostedFile.InputStream,fuAttachments.PostedFile.ContentType);

                at.ContentDisposition.FileName = this.fuAttachments.FileName;
                msg.Attachments.Add(at);


            }
           smtp.EnableSsl = true;

            msg.From = new MailAddress(strFrom);
            msg.To.Add(strTo);
            msg.Subject = strSubject;
            msg.Body = strBody;

            //smtp = new SmtpClient("localhost");
            //smtp.UseDefaultCredentials = true;

            try 
            {
                smtp.Send(msg);
            }
            catch (SmtpException Ex)
            {

                throw;
            } 

            if (msg.Attachments.Count > 0)
            {
                //Clear the attachments and delete the sessionid folder from tempFiles 
                msg.Attachments.Dispose();
            }

        }

5 个答案:

答案 0 :(得分:1)

它具有上传文件的默认限制,请参阅此链接以解决此问题

http://frazsundal.blogspot.com/2011/02/request-filtering-module-is-configured.html

答案 1 :(得分:1)

web.config

添加此行

<system.web>
   <httpRuntime maxRequestLength="10000" />
</system.web>

maxRequestLength="10000"使您的应用上传最大尺寸为10mb。

答案 2 :(得分:1)

我在我的应用程序中找到了罪魁祸首。它是smtp客户端的超时属性,在大约1.5分钟后停止进程。此属性的默认值为100秒,我更改为1500秒(1500000ms,因为此属性取值为ms)并成功邮寄附件。

smtp.Timeout = 1500000;

有关详情,请参阅this

顺便说一下,我用 21mb 附件进行了测试。

答案 3 :(得分:0)

尝试使用httpRuntime节点的maxRequestLength。可能还需要更改executionTimeout。

答案 4 :(得分:0)

这听起来类似于此问题SmtpClient.Send attachment maximum size,因此您的SMTP服务器可能存在问题 - 尝试检查设置。

相关问题