UseStaticFiles OnPrepareResponse在运行时切换流

时间:2018-12-08 21:28:29

标签: c# asp.net-core

是否可以用另一个图像流替换原始图像流?

似乎我无法将sr分配给HttpResponse

使用asp core 2

有可能这样做吗?或者还有其他方法可以做到吗?

using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace ConsoleApp3
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            var myconn = new MySqlConnection
            ("server = 0.0.0.0;" +
            "user = user;" +
            "database = sys;" +
            "port = 0000;" +
            "password = password;" +
            "Connect Timeout=1;");

            myconn.Open();

            List<MailItem> mailItems = readPst(@"C:\Users\john\Desktop\working.pst", "working");
            int counter = 0;
            int totalMailItemCount = mailItems.Count();
            for (int i = 0; i < mailItems.Count(); ++i)// MailItem mailItem in mailItems)
            {
                var dateTimeSentToInsert = mailItems[i].SentOn.ToString("yyyy-MM-dd H:mm:ss");
                var dateTimeReceivedToInsert = mailItems[i].ReceivedTime.ToString("yyyy-MM-dd H:mm:ss");
                var recipients = "";
                foreach (Recipient recipient in mailItems[i].Recipients)
                {
                    recipients += recipient.Address + "~";
                }

                MySqlCommand command = myconn.CreateCommand();
                command.CommandText = "INSERT INTO mail2 (Sender_Name, Sender_Email, Received_Name, Received_Email, Date_Sent, Date_Received, Subject, Body" +
                    ") VALUES (@sender_name, @sender_email, @received_name, @received_email, @date_sent, @date_received, @subject, @body)";
                command.Parameters.AddWithValue("@sender_name", mailItems[i].SenderName);
                command.Parameters.AddWithValue("@sender_email", mailItems[i].SenderEmailAddress);
                command.Parameters.AddWithValue("@received_name", mailItems[i].ReceivedByName);
                command.Parameters.AddWithValue("@received_email", recipients);
                command.Parameters.AddWithValue("@date_sent", dateTimeSentToInsert);
                command.Parameters.AddWithValue("@date_received", dateTimeReceivedToInsert);
                command.Parameters.AddWithValue("@subject", mailItems[i].Subject);
                command.Parameters.AddWithValue("@body", mailItems[i].Body);
                command.ExecuteNonQuery();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItems[i]);

                Console.WriteLine(++counter + " completed out of " + totalMailItemCount);
            }

            myconn.Dispose();
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }

    private static List<MailItem> readPst(string pstFilePath, string pstName)
    {
        List<MailItem> mailItems = new List<MailItem>();
        Application app = new Application();
        NameSpace outlookNs = app.GetNamespace("MAPI");
        // Add PST file (Outlook Data File) to Default Profile
        MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
        // Traverse through all folders in the PST file
        // TODO: This is not recursive, refactor
        Folders subFolders = rootFolder.Folders;
        foreach (Folder folder in subFolders)
        {
            Items items = folder.Items;
            foreach (object item in items)
            {
                if (item is MailItem)
                {
                    MailItem mailItem = item as MailItem;
                    mailItems.Add(mailItem);
                }
            }
        }
        // Remove PST file from Default Profile
        //outlookNs.RemoveStore(rootFolder);
        return mailItems;
    }
}

1 个答案:

答案 0 :(得分:0)

这行得通...

        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = x =>
            {   
                if (x.Context.Request.Path.Value.StartsWith("/images/"))
                {
                    using (var sr = File.OpenRead(path))
                    {             
                        HttpResponse hr = x.Context.Response;
                        hr.ContentLength = sr.Length;
                        sr.CopyTo(x.Context.Response.Body);
                    }
                }
            }
        });