到目前为止,我有一个表格,它被填写了,有 3 个可能的文件可以上传,既可以保存为数据库条目,也可以作为电子邮件发送,文件作为附件。
我没有立即注意到它,但我想我错过了一些东西,因为尽管文件获得了正确的类型和名称,但它们都没有内容,文件大小为 64b。
创建页面相关内容
@using (Html.BeginForm("Create", "RequestForm", FormMethod.Post, new { id = "ctFRM", enctype = "multipart/form-data", @role = "form" }))
{
<div class="row">
<div class="col-xs-12">
<label>Attachments</label>
</div>
<div class="col-xs-11" style="border: 1px solid #dedede; border-radius: 5px; padding: 5px; margin: 0 15px; ">
<input type="file" name="AttachmentId1" /><br />
<input type="file" name="AttachmentId2" /><br />
<input type="file" name="AttachmentId3" />
</div>
</div>
控制器操作相关内容
[HttpPost]
public ActionResult Create(SDRequestVM sdvm, FormCollection fc)
{
SDRequest sdr = sdvm.sdr;
MailMessage msg = new MailMessage();
#region Attachment Code
if (Request != null)
{
HttpPostedFileBase crfile = Request.Files["AttachmentId1"];
if ((crfile != null) && (crfile.ContentLength > 0) && !string.IsNullOrEmpty(crfile.FileName))
{
string fileName = crfile.FileName;
string fileContentType = crfile.ContentType;
byte[] fileBytes = new byte[crfile.ContentLength];
crfile.InputStream.Read(fileBytes, 0, Convert.ToInt32(crfile.ContentLength));
FileLibrary fl = new FileLibrary();
fl.FileName = fileName;
fl.FileType = fileContentType;
fl.FileContent = fileBytes;
fl.CreatedAt = DateTime.Now;
fl.CreatedBy = HttpContext.Request.LogonUserIdentity.User.Value;
db.FileLibraries.Add(fl);
db.SaveChanges();
sdr.AttachmentId1 = fl.Id;
msg.Attachments.Add(new Attachment(crfile.InputStream, crfile.FileName));
}
HttpPostedFileBase od1 = Request.Files["AttachmentId2"];
if ((od1 != null) && (od1.ContentLength > 0) && !string.IsNullOrEmpty(od1.FileName))
{
string fileName = od1.FileName;
string fileContentType = od1.ContentType;
byte[] fileBytes = new byte[od1.ContentLength];
od1.InputStream.Read(fileBytes, 0, Convert.ToInt32(od1.ContentLength));
FileLibrary fl = new FileLibrary();
fl.FileName = fileName;
fl.FileType = fileContentType;
fl.FileContent = fileBytes;
fl.CreatedAt = DateTime.Now;
fl.CreatedBy = HttpContext.Request.LogonUserIdentity.User.Value;
db.FileLibraries.Add(fl);
db.SaveChanges();
sdr.AttachmentId2 = fl.Id;
msg.Attachments.Add(new Attachment(od1.InputStream, od1.FileName));
}
HttpPostedFileBase od2 = Request.Files["AttachmentId3"];
if ((od2 != null) && (od2.ContentLength > 0) && !string.IsNullOrEmpty(od2.FileName))
{
string fileName = od2.FileName;
string fileContentType = od2.ContentType;
byte[] fileBytes = new byte[od2.ContentLength];
od2.InputStream.Read(fileBytes, 0, Convert.ToInt32(od2.ContentLength));
FileLibrary fl = new FileLibrary();
fl.FileName = fileName;
fl.FileType = fileContentType;
fl.FileContent = fileBytes;
fl.CreatedAt = DateTime.Now;
fl.CreatedBy = HttpContext.Request.LogonUserIdentity.User.Value;
db.FileLibraries.Add(fl);
db.SaveChanges();
sdr.AttachmentId3 = fl.Id;
msg.Attachments.Add(new Attachment(od2.InputStream, od2.FileName));
}
}
#endregion Attachment Code
保存/通过电子邮件发送控制器代码
db.SDRequests.Add(sdr);
db.SaveChanges();
SmtpClient c = new SmtpClient();
c.Send(msg);
return RedirectToAction("Confirmation");
网络配置邮件设置
<system.net>
<mailSettings>
<smtp from="ExampleAddress@example.example3.uk">
<network host="smtp.example2.example3.uk" port="25" />
</smtp>
</mailSettings>
</system.net>
据我所知,除非我在文件中犯了错误,否则它应该可以正常工作。输入流,作为新附件添加到消息中。
任何帮助将不胜感激。
你能在web.config中试试这一行吗?
<system.web>
<httpRuntime maxRequestLength="10240" />
</system.web>