我有一个关于 MVC 4 C# 互联网应用程序的项目,我按照一些指南使用 ajax 函数将 Google 可视化柱形图保存到我的驱动器中。此外,我正在使用 razor PDF nugget 包制作 PDF 格式的报告。路径是: “C\temp\”和两个文件都在那里。我需要将两个文件附加到电子邮件中并发送它们,然后删除创建的文件。
我用 ajax 调用发送电子邮件功能:
$('#btnSend').on('click', function () {
if (from) {
fromDate = from.toJSON();
}
if (to) {
toDate = to.toJSON();
}
// from and to are dates taken from two datepickers
// saves the pdf to the server
if (from && to) {
$.ajax({
url: "/Home/SavePdf/",
data: { fromDate: fromDate, toDate: toDate },
type: 'GET',
async: false,
datatype: 'json',
success: function (data) {
alert('got here with data');
},
error: function () { alert('something bad happened'); }
});
}
// saves the column chart to the server from canvas item with id reports
var imgData = getImgData(document.getElementById("reports"));
var imageData = imgData.replace('data:image/png;base64,', '');
$.ajax({
url: "/Home/SaveImage/",
type: 'POST',
data: '{ "imageData" : "' + imageData + '" }',
datatype: 'json',
async: false,
contentType: 'application/json; charset=utf-8',
success: function () {
alert('Image saved successfully !');
},
error: function () {
alert('something bad happened');
}
});
$.ajax({
type: "POST",
url: "/Home/SendMail/",
async: false,
success: function() {
alert('Message sent successfully');
}
});
});
这是我在后面代码中的功能
public void SendMail()
{
var path = @"C:\temp\";
string pngFilePath = path + DateTime.Now.ToShortDateString() + ".png";
string pdfFilePath = path + DateTime.Now.ToShortDateString() + ".pdf";
MailMessage message = new MailMessage("message sender", "message reciever")
{
Subject = "Test",
Body = @"Test"
};
Attachment data = new Attachment(pdfFilePath ,
MediaTypeNames.Application.Pdf);
message.Attachments.Add(data);
Attachment data2 = new Attachment(pngFilePath , GetMimeType(pngFilePath);
// GetMimeType function gets the mimeType of the unknown attachment
message.Attachments.Add(data2);
SmtpClient client = new SmtpClient();
client.Host = "smtp.googlemail.com";
client.Port = 587;
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("mail sender", "sender password");
client.Send(message);
// delete the png after message is sent
if ((System.IO.File.GetAttributes(pngFilePath ) & FileAttributes.Hidden) == FileAttributes.ReadOnly)
{
System.IO.File.SetAttributes(pngFilePath , FileAttributes.Normal);
if (System.IO.File.Exists(pngFilePath ))
{
System.IO.File.Delete(pngFilePath );
}
}
// delete the pdf after message is sent
if ((System.IO.File.GetAttributes(pdfFilePath ) & FileAttributes.Hidden) == FileAttributes.ReadOnly)
{
System.IO.File.SetAttributes(pdfFilePath , FileAttributes.Normal);
if (System.IO.File.Exists(pdfFilePath ))
{
System.IO.File.Delete(pdfFilePath );
}
}
}
我想删除这些文件,但 IIS 继续使用它们并且永远无法执行删除操作。有没有办法从 iis 中分离这些文件并删除它们?