簡體   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