简体   繁体   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

I am using this method for saving the file on the server I want to know that if this method is using by multiple users at the same time,我正在使用这种方法将文件保存在服务器上我想知道如果这个方法被多个用户同时使用,

more information is that: this method is called from the JsonResult of asp.net MVC application and this method saves the file on the server and return path to the JsoneResult such path is saved in database.更多信息是:此方法是从 asp.net MVC 应用程序的 JsonResult 调用的,此方法将文件保存在服务器上,并将 JsoneResult 的返回路径保存在数据库中。

1: is it a safe way to store files even multiple users are online at the same time. 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;
    }

If you're asking if the variables within the method could be shared between two callers simultaneously, the answer is no.如果您询问方法中的变量是否可以同时在两个调用者之间共享,答案是否定的。 Variables within a static method are not shared across calls - only static properties of a class are shared. static 方法中的变量不会在调用之间共享 - 只有class 的 static 属性是共享的。

If two callers call the method with exactly the same parameters at exactly the same time, then they could either write over each other, or the second could get an exception that the file exists, or something else if SaveAs is not thread-safe.如果两个调用者在完全相同的时间使用完全相同的参数调用该方法,那么他们可能会相互覆盖,或者第二个调用者可能会收到文件存在的异常,或者如果SaveAs不是线程安全的,则会出现其他情况。

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

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