【发布时间】:2021-04-12 20:50:44
【问题描述】:
我正在使用这种方法将文件保存在服务器上我想知道这个方法是否被多个用户同时使用,
更多信息是:从asp.net MVC应用程序的JsonResult调用该方法,该方法将文件保存在服务器上,并将JsoneResult的返回路径保存在数据库中。
1:即使多个用户同时在线也能安全地存储文件吗?
public static string SaveImageToServer(dynamic Image, string FolderPath, string FolderRootPath)
{
if (Image != null)
{
HttpPostedFileBase _image = (HttpPostedFileBase)Image;
string fileExtention = _image.FileName.Substring(_image.FileName.LastIndexOf('.'));
FUN.CreateMosDocInnerDir(FolderPath.ToString());
string ImgName = DateTime.Now.ToString("MMddyyyyHHmmssfffff");
string CreatedFileCompletePath = "/" + FolderRootPath + "/" + FolderPath.ToString() + "/" + FolderPath.ToString() + "_" + ImgName + fileExtention;
string path = HttpContext.Current.Server.MapPath("~" + CreatedFileCompletePath);
_image.SaveAs(path);
return CreatedFileCompletePath;
}
return null;
}
【问题讨论】:
-
如果问题是“两个用户可以得到相同的文件名吗?”答案是肯定的。但这并不是因为该方法是静态的。这是因为您使用时间作为文件名,如果两个电话几乎同时进行,则可能是相同的。
-
如果2个或2个以上用户同时调用该函数,则第2个参数和第3个参数不同。
-
1:是否有可能两个用户同时调用并且都得到相同的**返回结果**? 2:是否有可能user1文件将保存在服务器上而user2文件不会保存在服务器上并且两个用户都得到了相同的结果。
-
如果这些参数不同,那么没有。静态方法不共享其中声明的变量。
标签: c# .net static static-methods