繁体   English   中英

使用 static 方法为多个用户同时保存文件,如果多个用户同时保存文件,这种 static 方法是否更安全

[英]file save with static method for multiple users at the same time , is this static method is safer if multiple user save the file at the same time

我正在使用这种方法将文件保存在服务器上我想知道如果这个方法被多个用户同时使用,

更多信息是:此方法是从 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;
    }

如果您询问方法中的变量是否可以同时在两个调用者之间共享,答案是否定的。 static 方法中的变量不会在调用之间共享 - 只有class 的 static 属性是共享的。

如果两个调用者在完全相同的时间使用完全相同的参数调用该方法,那么他们可能会相互覆盖,或者第二个调用者可能会收到文件存在的异常,或者如果SaveAs不是线程安全的,则会出现其他情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM