【发布时间】:2018-10-07 14:22:10
【问题描述】:
我在这里使用 WebApi 我想要一张用于发送电子邮件的图像为此我将代码编写为:
var files = HttpContext.Current.Request.Files;
if (files.Count > 0) {
for (int i = 0; i < files.Count; i++) {
HttpPostedFile file = files[i];
mailModel.filename = file.FileName;
mailModel.filecontent = file.InputStream;
}
}
这里如何绑定mailModel.Filecontent
我的类文件为
public class SendMailRequest
{
public string filecontent { get; set; }
public string filename { get; set; }
}
我的邮件发送代码是:
if (mailModel.filename != null) {
string tempPath = WebConfigurationManager.AppSettings["TempFile"];
string filePath = Path.Combine(tempPath, mailModel.filename);
using(System.IO.FileStream reader = System.IO.File.Create(filePath)) {
byte[] buffer = Convert.FromBase64String(mailModel.filecontent);
reader.Write(buffer, 0, buffer.Length);
reader.Dispose();
}
msg.Attachments.Add(new Attachment(filePath));
如何将我的文件绑定到 FileContent?
【问题讨论】:
-
mailModel.filecontent = file.InputStream;这行肯定不行,因为InputStream是Stream,而不是string? -
@PrashantPimpale 你有任何想法将 HttpPostedFile 转换为字符串.. 因为这里我需要 2 将数据转换为 Base64
标签: c# asp.net-web-api httppostedfilebase