0

我使用此代码上传单个文件。

jQuery 阿贾克斯:

$(document).ready(function () {
    $('input[type=file]').change(function () {
        $(this).simpleUpload("/Admin/News/GetFile", {
            start: function (file) {
                //upload started
                console.log("upload started");
            },
            progress: function (progress) {
                //received progress
                console.log("upload progress: " + Math.round(progress) + "%");
            },
            success: function (data) {
                //upload successful
                console.log("upload successful!");
                console.log(data);
            },
            error: function (error) {
                //upload failed
                console.log("upload error: " + error.name + ": " + error.message);
            }
        });
    });
});

简单的上传脚本。

控制器 :

public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile)
{
     //Request.Files[0];
     if (NewsDefaultFile != null)
     {
         Session.Add("File", NewsDefaultFile);
     }
     return Content("");
}

[HttpPost]
    public ActionResult CreateNews(NewsModel model)
    {
        if (model.NewsDefaultFile == null)
        {
            var File = (HttpPostedFileBase)Session["File"];
            if (File.ContentLength > 0)
            {
                var ext = System.IO.Path.GetExtension(File.FileName);
                if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
                {
                    string filename = model.NewsTitle + _NewsClass.Rand();
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                }
                if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                {
                    string filename = _NewsClass.Rand() + model.NewsTitle;
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                }
                if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                {
                    string filename = _NewsClass.Rand() + model.NewsTitle;
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                }
            }
        }
    }

现在我需要使用此代码上传多个文件,该怎么做?

4

2 回答 2

0

尝试如下它可能会有所帮助。

[HttpPost]
public ActionResult GetFile(List<HttpPostedFileBase> NewsDefaultFile)
{
    if (NewsDefaultFile != null)
    {
       Session["Files"] = NewsDefaultFile;
    }     
    return Content("");
}

[HttpPost]
public ActionResult CreateNews(NewsModel model)
{
    if (model.NewsDefaultFile == null && Session["Files"] != null)
    {
        // Retrieve in Session
        var files = (List<HttpPostedFileBase>)Session["Files"];
        var i = 1; // Just to change a bit the filename

        foreach (var File in files)
        {
            //write your own logic here 
            if (File.ContentLength > 0)
            {
                var ext = System.IO.Path.GetExtension(File.FileName);
                if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
                {
                    // Just added $"_{i}" to avoid overriding saved file
                    string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}";
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                    i++;
                }
                if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                {
                    // Just added $"_{i}" to avoid overriding saved file
                    string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                    i++;
                }
                if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                {
                    // Just added $"_{i}" to avoid overriding saved file
                    string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
                    File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
                    model.NewsDefaultFile = filename;
                    model.NewsDefaultFileExt = ext;
                    i++;
                }
            }
        }    
    }
}
于 2017-02-20T11:15:55.773 回答
0

我想你只需要改变你的类型Session["File"]来使用 aList<HttpPostedFileBase>而不是HttpPostedFileBase.

当您要求使用提供的代码执行此操作时。

更改您的GetFile操作代码:

public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile)
{
    //Request.Files[0];
    if (NewsDefaultFile != null)
    {
        // Retrieve the list from session or create a new one
        var files = Session["Files"] == null ? 
            new List<HttpPostedFileBase>() : 
            (List<HttpPostedFileBase>)Session["Files"];

        // Add the posted file
        files.Add(NewsDefaultFile);

        // Store the list in session
        Session["Files"] = files;
     }
     return Content("");
}

并更改您的CreateNews操作代码:

[HttpPost]
public ActionResult CreateNews(NewsModel model)
{
    if (model.NewsDefaultFile == null)
    {
        if (Session["Files"] != null)
        {
            // Retrieve in Session
            var files = (List<HttpPostedFileBase>)Session["Files"];
            var i = 1; // Just to change a bit the filename

            foreach (var File in files)
            {
                if (File.ContentLength > 0)
                {
                    var ext = System.IO.Path.GetExtension(File.FileName);
                    if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
                    {
                        // Just added $"_{i}" to avoid overriding saved file
                        string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}";
                        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
                        model.NewsDefaultFile = filename;
                        model.NewsDefaultFileExt = ext;
                        i++;
                    }
                    if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                    {
                        // Just added $"_{i}" to avoid overriding saved file
                        string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
                        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
                        model.NewsDefaultFile = filename;
                        model.NewsDefaultFileExt = ext;
                        i++;
                    }
                    if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
                    {
                        // Just added $"_{i}" to avoid overriding saved file
                        string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
                        File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
                        model.NewsDefaultFile = filename;
                        model.NewsDefaultFileExt = ext;
                        i++;
                    }
                }
            }
        }
    }
}
于 2017-02-20T10:55:53.473 回答