【问题标题】:Could not find file 'C:\Program Files (x86)\IIS Express\GraceCancerFoundation.png'找不到文件“C:\Program Files (x86)\IIS Express\GraceCancerFoundation.png”
【发布时间】:2020-01-29 07:11:46
【问题描述】:

我想将上传的文件发送到邮箱。这是我的 asp.net 代码。我收到错误

for (int x = 0; x < Request.Files.Count; x++)
{
    HttpPostedFile PostedFile = Request.Files[x];
    if (PostedFile != null && PostedFile.ContentLength > 0)
    {
        string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
        string FilePath = System.IO.Path.GetFullPath(PostedFile.FileName);
        try
        {
            message.Attachments.Add(new Attachment(FilePath));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

错误:

找不到文件 'C:\Program Files (x86)\IIS Express\GraceCancerFoundation.png'。实际路径: E:\GraceCancerFoundation.png

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您已收到请求中的文件,将其保存到磁盘,然后将其添加为附件。

    相反,您可以只将流传递给附件而不是路径

    for (int x = 0; x < Request.Files.Count; x++)
            {
                HttpPostedFile PostedFile = Request.Files[x];
                if (PostedFile != null && PostedFile.ContentLength > 0)
                {
                    try
                    {
                        message.Attachments.Add(new Attachment(PostedFile.InputStream,  
                                                               PostedFile.FileName,
                                                               System.Net.Mime.MediaTypeNames.Application.Octet));
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
    

    【讨论】:

    • 我试过这个,但它显示“System.Net.Mail.Attachment.Attachment(string)”的最佳重载方法匹配有一些无效参数
    • 从邮件中下载上传的文件后。我没有看到任何数据,表明我们不支持这种文件格式
    • 不,我仍然收到相同的消息“看起来我们不支持这种文件格式。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多