【问题标题】:ASP.NET MVC5 file upload issueASP.NET MVC5 文件上传问题
【发布时间】:2018-07-28 02:13:42
【问题描述】:

我正在开发一个应用程序,其中 api 服务器、数据库服务器和 Web 服务器是不同的。我们创建一个服务器来存储文件并创建虚拟目录来访问所有文件。但我的问题是如何存储文件这个位置。我的网络位置是 \10.0.0.51\CB-Clients 我怎样才能将文件存储在这个位置。我的代码是

string _path =Path.Combine("\\10.0.0.51\CB-Clients\", "abc.png");
 FileStream newFile = new FileStream(_path, FileMode.Create);
newFile.Write(userWiseDocumentStorage.DocImageByte, 0, userWiseDocumentStorage.DocImageByte.Length);
newFile.Close();

但问题是文件没有上传这个位置

【问题讨论】:

标签: c# asp.net asp.net-mvc


【解决方案1】:

按照这些步骤并尝试.... 1.在AppSettings里面的WebConfig文件中添加Location,比如

  <appSettings>
        <add key="DocumentationLocation" value="D:\CB-Clients" />
  </appSettings>

2.然后在 Controller Action 中尝试此代码,例如:

     public ActionResult FileSave(HttpPostedFileBase file)
                {

                    string _FileName = null;
                    string _path = null;                   
                    _FileName = Path.GetFileName(file.FileName);
                    string ext = Path.GetExtension(_FileName);
                    string file1 = _FileName.Replace(" ", "");

                    if (ext.ToLower() == ".pdf" || ext.ToLower() == ".doc" || ext.ToLower() == ".jpg" || ext.ToLower() == ".docx" || ext.ToLower() == ".xls" || ext.ToLower() == ".xlsx" || ext.ToLower() == ".jpeg" || ext.ToLower() == ".png")
                    {

                        string location = (System.Configuration.ConfigurationSettings.AppSettings["DocumentationLocation"]);
                        _path = Path.Combine(location, file1);
                        if (System.IO.File.Exists(_path))
                        {
                            System.IO.File.Delete(_path);
                            file.SaveAs(_path);
                        }
                        else
                        {
                            file.SaveAs(_path);
                        }
                    }
                    return View();

                }

【讨论】:

    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2016-08-27
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多