【问题标题】:How to store array of path string in Tempdata MVC 4如何在 Tempdata MVC 4 中存储路径字符串数组
【发布时间】:2014-01-01 09:01:18
【问题描述】:

我的操作结果如代码中一样,从视图中逐个文件地返回图像

[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++)
                        {

                        }
                    }
                }                    
            }
        }

我想在临时数据中存储每个转弯的路径 说我有三张图片,动作将进入此方法三次,每次都带有下一张图片的路径 但我在中间迷路了。

【问题讨论】:

  • 问题不够清楚,无法回答,请指正。
  • 你从 Action 中返回了什么?
  • 带图片的动作
  • 您有一个索引为i 的for 循环嵌套在另一个索引为i 的for 循环中。很确定那是行不通的。
  • 不仅不清晰,甚至不是正常的代码。这是什么?

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


【解决方案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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多