如何在Tempdata MVC 4中存储路径字符串数组

时间:2014-01-01 09:01:18

标签: c# asp.net-mvc json asp.net-mvc-3 asp.net-mvc-4

我的动作结果如在代码中从视图

按文件返回文件
[HttpPost]       
    public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
    {
        if (!Directory.Exists(Server.MapPath("~/TempImages/")) || files != null)
        {
            string TempPath = Server.MapPath("~/TempImages/");
            string[] myTempPaths = new string[files.Count()];
            foreach (HttpPostedFileBase file in files)
            {
                string filePath = Path.Combine(TempPath, file.FileName);
                System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
                file.SaveAs(filePath);
                for (int i = 0; i < files.Count(); i++)
                {                        
                    myTempPaths = filePath.Split(',');
                    for (int j = 0; j < files.Count(); j++)
                    {
                        TempData["lev1"] = myTempPaths;
                        for (int k = 0; k < files.Count(); i++)
                        {

                        }
                    }
                }                    
            }
        }

我希望在tempdata中存储每个回合的路径 说我有三张图片,动作将输入此方法三次,每次都使用下一张图片的路径 但我在中间迷路了。

1 个答案:

答案 0 :(得分:1)

这就是我从你那里得到的......不管它是什么。 你想保存文件,然后返回他们的路径?!

[HttpPost]       
public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
{
    // reject if no file is send
    if(files == null || !files.Any())
    {
        // return error or throw exception
    }

    // create folder if not exist
    if(!Directory.Exists(Server.MapPath("~/TempImages/")))
    {
        // create folder
    }

    // base path for images
    string TempPath = Server.MapPath("~/TempImages/");
    // list to save file paths
    List<string> myTempPaths = new List<string>();


    foreach (HttpPostedFileBase file in files)
    {
        string filePath = Path.Combine(TempPath, file.FileName);

        // save file
        file.SaveAs(filePath);

        // add path to list
        myTempPaths.Add(filePath);
    }

    // save paths into temp Data
    TempData["lev1"] = myTempPaths;

    return View();
}