简体   繁体   中英

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.

1: is it a safe way to store files even multiple users are online at the same time.

    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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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