上传文件到服务器

    public string SaveServerFile(string tempfilename, string filepath, string filename, bool isLocal = false)
        {
            #region MyRegion
            bool UseWeb = bool.Parse(ConfigurationManager.AppSettings["WebFileService"]);
 
            //判断是否启用文件服务器
            if (UseWeb && !isLocal)
            {
                //存储临时文件
                string uri = GetBaseServerSaveFilePath() + filepath;
                string urishow = GetBaseServerFilePath() + filepath;
 
                try
                {
                    if (uri.StartsWith("http"))
                    {
                        WebClient webClient = new WebClient();
                        webClient.UploadFile(uri, "POST", tempfilename);
                        File.Delete(tempfilename);//删除临时上传文件
                        return urishow;
                    }
                    else
                    {
                        string userName = ConfigurationManager.AppSettings["ftpuser"];
                        string userPwd = ConfigurationManager.AppSettings["ftppwd"];
 
                        FtpHelper webClient = new FtpHelper(uri, userName, userPwd);
                        webClient.Upload(tempfilename);
                        //File.Delete(tempfilename);//删除临时上传文件
                        return urishow;
                    }
                }
                catch (Exception exception)
                {
                    return exception.Message;
                }
            }
            else
            {
                if (!filepath.EndsWith("/"))
                {
                    // filepath += "/";
                }
                string savefile = GetBaseServerFilePath(isLocal);
                if (!savefile.EndsWith("/") || (filepath.IsNotNullOrEmpty() && !filepath.StartsWith("/")))
            
                    savefile += filepath;
                if (!Directory.Exists(savefile))
                    Directory.CreateDirectory(savefile);
                savefile += filename;
                filepath += savefile;
             
            }
 
            #endregion
 
            return filepath;
 
 
        }

 

posted @ 2020-04-20 17:15  tommy~hi  阅读(145)  评论(0编辑  收藏  举报