到目前为止,我有一个表格可以填写并有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");
Webconfig邮件设置
<system.net>
<mailSettings>
<smtp from="ExampleAddress@example.example3.uk">
<network host="smtp.example2.example3.uk" port="25" />
</smtp>
</mailSettings>
</system.net>
据我所知,逐步完成代码它应该可以工作,除非我在file.InputStream中犯了一个错误,它被添加为msg的新附件。
非常感谢任何帮助。
答案 0 :(得分:0)
你可以在web.config中试试这行吗?
<system.web>
<httpRuntime maxRequestLength="10240" />
</system.web>