【问题标题】:FileUpload is not uploading all ImagesFileUpload 没有上传所有图片
【发布时间】:2018-05-02 07:22:22
【问题描述】:

我正在做一个项目,我必须使用 FileUpload 控件上传多个文件:

我有一个按钮可以像这样保存图像文件:

protected void btnSave_Click(object sender, EventArgs e)
{
    if (fuImage.HasFiles)
    {
        foreach (var file in fuImage.PostedFiles)
        {
            UploadFile("Images",fuImage);
        }
    }

}

我使用一种方法将文件上传/保存到文件夹中,如下所示:

private void UploadFile(string FolderName, FileUpload fu)
{
    string FolderPath = "~\\" + FolderName;

    DirectoryInfo FolderDir = new DirectoryInfo(Server.MapPath(FolderPath));
    if (!FolderDir.Exists)
    {
        FolderDir.Create();
    }

    string FilePath = Path.Combine(Server.MapPath(FolderPath), fu.FileName);
    if (!File.Exists(FilePath))
    {
        fu.SaveAs(FilePath);
    }
}

我面临的问题是 - 只上传一个图像文件,而不是像这样上传所有图像:

任何帮助将不胜感激!

【问题讨论】:

    标签: c# asp.net .net file-upload


    【解决方案1】:

    您的代码中有错误。这是一个固定的代码。

    首先确保您在asp.net 4.5 或更高版本上构建页面

    web.config

    <compilation debug="true" targetFramework="4.6.1" urlLinePragmas="true"/>
    

    接下来是你的.cs

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (fuImage.HasFiles)
        {
            foreach (HttpPostedFile file in fuImage.PostedFiles) //be explicit
            //Note that var file is HttpPostedFile
            {
                UploadFile("Images",file); //not fuImage
            }
        }
    }
    
    private void UploadFile(string FolderName, HttpPostedFile fu)//FileUpload gives single file
    {
        string FolderPath = "~/" + FolderName; // \\ is not the best choice
        //your remaining code works and is skipped for brevity
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 2023-03-17
      • 2018-02-17
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多