C# asp文件上传控件,应该使用哪一个?

C# asp文件上传控件,应该使用哪一个?,c#,asp.net,file-upload,C#,Asp.net,File Upload,好的,我在网上找到了这个来上传一些文件 if (FileUpload1.HasFile) { //create the path to save the file to string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName); //save the file to our local path FileUpload1.SaveAs(fileName); } 还有这个

好的,我在网上找到了这个来上传一些文件

if (FileUpload1.HasFile)
{
    //create the path to save the file to
    string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);
    //save the file to our local path
    FileUpload1.SaveAs(fileName);
}
还有这个

//check to make sure a file is selected
if (FileUpload1.HasFile)
{
    //create the path to save the file to
    string fileName = Path.Combine(Server.MapPath("~/Files"), FileUpload1.FileName);
    //save the file to our local path
    FileUpload1.SaveAs(fileName);
}

有什么区别,用哪一个?我搞糊涂了。顺便问一下,若我可以在数据库中存储文件路径,下次当我想删除或查看该文件时,如何检索该文件?比如说,首先我向数据库添加了一条记录,并上传了一个.doc文件/excel文件,下次当我想编辑该记录时,我想检索上传的文件,并在UI中显示它。谢谢。

您发布的两个代码块的唯一区别在于指定文件路径

在案例1中,指定静态位置以保存文件。若在生产环境中保存文件的位置不同,则可能会导致问题。在这种情况下需要重建


而在案例2中,位置是使用相对路径指定的。因此,它总是将文件保存在“/files”位置。

使用第二个,因为它会将相对或虚拟路径转换为实际路径本身。你应该从数据库中获取路径,并使用它来解析路径,方法与你存储路径的方式相同,并对其进行操作删除等,以显示url=“~/Files/yourfilename” yourfilefromdb-u从数据库中检索它

string filepath = Path.Combine(Server.MapPath("~/Files"), yourfilefromdb);
File.Delete(filepath);

for showing
if it accessible directly u can just write url="~/Files/yourfilefromdb"

使用第二个,因为它会将相对路径或虚拟路径转换为实际路径本身。您应该从db获取路径,并使用它以与存储相同的方式解析路径,并对其执行操作删除等。对于显示url=“~/Files/yourfilename”,如果您已经知道您的文件夹是:E:\ftproot\sales,则无需使用Server.MapPath,如果您只有一个相对的虚拟路径,如~/folder/folder1,并且您想知道磁盘中的实际路径,那么需要最后一个路径…嗯,这意味着最好使用第二个路径?
//if you already know your folder is: E:\ABC\A then you do not need to use Server.MapPath, this last one is needed if you only have a relative virtual path like ~/ABC/A and you want to know the real path in the disk...

    if (FileUpload1.HasFile)
    {
        string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);// they know the right path so .they using directly  
        FileUpload1.SaveAs(fileName);
    }

    if (FileUpload1.HasFile)
    {
        string fileName = Path.Combine(Server.MapPath("~/Files"), FileUpload1.FileName);// i don't know path is correct or not so they using Server.MapPath. . .
        FileUpload1.SaveAs(fileName);
    }