Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#,ASP.NET MVC:多个图像上载和上载的图像都是相同的问题_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C#,ASP.NET MVC:多个图像上载和上载的图像都是相同的问题

C#,ASP.NET MVC:多个图像上载和上载的图像都是相同的问题,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我试图上传多张图片,我成功上传了我想要的图片。我唯一的问题是,我选择的所有图像都是通过复制第一个来复制的。我在代码中找不到错误 我的上传图像方法: public static string UploadImage(string serverPath, IEnumerable<HttpPostedFileBase> files) { Guid uniqueName = Guid.NewGuid(); serverPath = serverPath.Re

我试图上传多张图片,我成功上传了我想要的图片。我唯一的问题是,我选择的所有图像都是通过复制第一个来复制的。我在代码中找不到错误

我的
上传图像
方法:

public static string UploadImage(string serverPath, IEnumerable<HttpPostedFileBase> files)
{
        Guid uniqueName = Guid.NewGuid(); 
        serverPath = serverPath.Replace("~", string.Empty);
        string filePath;

        foreach (HttpPostedFileBase item in files)
        {
            if (files != null && item.ContentLength > 0)
            {
                string extension = Path.GetExtension(item.FileName);
                string fileName = $"{uniqueName}{extension}";

                if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".gif" || extension.ToLower() == ".png" || extension.ToLower() == ".jpg")
                {
                    if (File.Exists(HttpContext.Current.Server.MapPath(serverPath + fileName))) 
                    {
                        return "Already exists from same file";
                    }
                    else
                    {
                        filePath = Path.Combine(HttpContext.Current.Server.MapPath(serverPath),fileName);
                        item.SaveAs(filePath);
                        return serverPath+fileName;
                    }
                }
                else
                {
                    return "Not the selected picture.";
                }
            }
            else
            {
                return "No File Selected";
            }    
    }

    return "";
}
以及
ImageClass

public class TestClass
{
    public int ID { get; set; }

    public string Name { get; set; }
    public string ImagePath { get; set; }
}
以及视图
Index.cshtml

@using (Html.BeginForm("Index","Image",FormMethod.Post, new { enctype="multipart/form-data"}))
{
<div>
     Name
</div>
<div>
    @Html.TextBoxFor(x=>x.Name)
</div>
<div>
    <input multiple type="file" name="files" value="Browse" />
</div>
<div>
    <button class="btn btn-primary">Save</button>
</div>
}
@使用(Html.BeginForm(“Index”,“Image”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
名称
@Html.TextBoxFor(x=>x.Name)
拯救
}

您的问题是,您只需生成一个uniquename,然后将其用于所有这些名称。 你应该写这行:

 Guid uniqueName = Guid.NewGuid(); 
在您的
for
循环中,如下所示:

 foreach (HttpPostedFileBase item in files)
    {
        if (files != null && item.ContentLength > 0)
        {
             Guid uniqueName = Guid.NewGuid(); 
            string extension = Path.GetExtension(item.FileName);
            string fileName = $"{uniqueName}{extension}";
           // the rest of your code
}
还有一些事情需要解决 1-在您的操作方法中,您上载了所有文件,但您的
UploadImage
方法只返回一个字符串,因此所有文件的
ImagePath
都是相等的。 您可以像下面这样重写它:

foreach (HttpPostedFileBase item in files)
    {
        var  fileObj = new TestClass();
        fileObj.ImagePath = ImageUploader.UploadImage("~/Images/", files) ;
        db.TestClass.Add(fileObj);
        db.SaveChanges();
    }
已解决

我意识到,我试图上传的照片之所以一直作为同一张照片上传,是因为索引号总是被重置

作为解决方案:我从ImageUploader静态方法中删除了foreach循环,并为静态类定义了一个int类型变量,每次ImageController中的foreach循环运行时,它都必须在静态方法上运行,并且每次都会递增一次。当我们想再次上传图像时,我们会在过程结束时将其返回到零,这样索引号就不会保持不变。当我们上传多张照片并将数据保存到数据库时,可以使用此解决方案。如果我们只想上传多张照片,应该使用foreach循环

public static class ImageUploader
{
    internal static int indexer;


public static string UploadImage(string serverPath, List<HttpPostedFileBase> files)
    {
        serverPath = serverPath.Replace("~", string.Empty);
        if (files != null || files[indexer].ContentLength < 0)
        {
            Guid uniqueName = Guid.NewGuid();
            string extension = Path.GetExtension(files[indexer].FileName);
            string fileName = $"{uniqueName}{extension}";
            if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".gif" || extension.ToLower() == ".png" || extension.ToLower() == ".jpg")
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(serverPath + fileName))) 
                {
                    return "Already Exists From Same File";
                }
                else
                {
                    string filePath = Path.Combine(HttpContext.Current.Server.MapPath(serverPath),fileName);
                    files[indexer].SaveAs(filePath);
                    indexer++;
                if (indexer == files.Count)
                {
                    indexer = 0;
                }
                return serverPath + fileName;
                }
            }
            else
            {
                return "Not the selected picture.";
            }
        }
        else
        {
            return "No File Selected";
        }
    }
}
公共静态类ImageUploader
{
内部静态int索引器;
公共静态字符串上载映像(字符串服务器路径,列表文件)
{
serverPath=serverPath.Replace(“~”,string.Empty);
if(files!=null | | files[indexer].ContentLength<0)
{
Guid uniqueName=Guid.NewGuid();
字符串扩展名=Path.GetExtension(文件[indexer].FileName);
字符串文件名=$“{uniqueName}{extension}”;
如果(extension.ToLower()=”.jpeg“| | extension.ToLower()=”.gif“| | extension.ToLower()=”.png“| | extension.ToLower()=”.jpg”)
{
if(File.Exists(HttpContext.Current.Server.MapPath(serverPath+fileName)))
{
返回“已存在于同一文件中”;
}
其他的
{
字符串filePath=Path.Combine(HttpContext.Current.Server.MapPath(serverPath),文件名);
文件[索引器].SaveAs(文件路径);
索引器++;
if(indexer==files.Count)
{
索引器=0;
}
返回serverPath+fileName;
}
}
其他的
{
返回“不是所选图片。”;
}
}
其他的
{
返回“未选择文件”;
}
}
}

}谢谢你的帮助,但是写这些代码并不能解决我的问题。 让我举例说明这个问题; 例如,我选择了四张图片,当我单击“保存”按钮时,它会使用新的Guid名称添加到数据库中,但第一张选定图片的文件扩展名仍保留在内存中。将四张图片添加到“图像”文件夹中,但图像都是相同的


替换代码没有帮助,而且此代码的工作方式符合我的要求。当您将其添加到数据库时,名称会更改,但文件扩展名仍保留为第一个选定文件的扩展名,并且所有图像都保持不变。您是否已使用chnage
Guid uniqueName=Guid.NewGuid()for
循环中的code>?我是否也应该在
foreach
循环中打开
for
循环?
public static class ImageUploader
{
    internal static int indexer;


public static string UploadImage(string serverPath, List<HttpPostedFileBase> files)
    {
        serverPath = serverPath.Replace("~", string.Empty);
        if (files != null || files[indexer].ContentLength < 0)
        {
            Guid uniqueName = Guid.NewGuid();
            string extension = Path.GetExtension(files[indexer].FileName);
            string fileName = $"{uniqueName}{extension}";
            if (extension.ToLower() == ".jpeg" || extension.ToLower() == ".gif" || extension.ToLower() == ".png" || extension.ToLower() == ".jpg")
            {
                if (File.Exists(HttpContext.Current.Server.MapPath(serverPath + fileName))) 
                {
                    return "Already Exists From Same File";
                }
                else
                {
                    string filePath = Path.Combine(HttpContext.Current.Server.MapPath(serverPath),fileName);
                    files[indexer].SaveAs(filePath);
                    indexer++;
                if (indexer == files.Count)
                {
                    indexer = 0;
                }
                return serverPath + fileName;
                }
            }
            else
            {
                return "Not the selected picture.";
            }
        }
        else
        {
            return "No File Selected";
        }
    }
}