ERROR: The SaveAs method is configured to require a rooted path, and the path '~/Documents/Upload/Abc.mdb' is not rooted

时间:2018-02-03 07:39:32

标签: c# asp.net

I m Trying to upload a mdb file in my database so my code is

if (fileUploadEspace.HasFile)
                {
                    try
                    {
                        string savePath = string.Empty;
                        savePath = ConfigurationManager.AppSettings["UploadDBFilePath"];
                        Server.ScriptTimeout = 100000000;


                        fileUploadEspace.SaveAs(savePath + fileUploadEspace.FileName);

in web.config file i kept the path as

 <add key="UploadDBFilePath" value="~/Documents/Upload/" />

but when I m clicking Upload buttton it is giving me above error message. I searched for the same error but still the error in not resolving.

1 个答案:

答案 0 :(得分:0)

SaveAs needs a physical path for writing the file, you will have to use Server.MapPath for generating it.

For creating path by combining the file name and the path use Path.Combine and then on that call Server.MapPath to get the Physical path and then SaveAs can write the file in that directory.

fileUploadEspace.SaveAs(Server.MapPath(Path.Combine(savePath,fileUploadEspace.FileName)));