---
# System prepended metadata

title: Problem
tags: [GoMore]

---

# Problem
要如何利用MVC架構將檔案從FTP下載或是讀取出來丟到View？
目前因為要用three.js去呈現3D環境，而顯示模型是透過讀取相對路徑或是input type=“file”內的檔案轉換成URL進行Loaded。

**設想架構：**
![](https://hackmd.io/_uploads/S1f4cz0Th.png)


以下是我的程式：
## 3D Viewer Environment
### javascript
(base on three.js)
```javascript=
const input = document.getElementById("fileurl");
const modelURL = URL.createObjectURL(input.value);
ifcLoader.load(modelURL, (ifcModel) => {
    scene.add(ifcModel);
});

```
## MVC架構
### cshtml
```javascript=
<form method="get" action="/Object/threeD">
    <div class="u-form-group u-form-select u-label-top u-form-group-1">
    <button name="ObjectName" value="@obj.ObjectName" class="btn btn-light">3D Viewer</button>
    <input type="hidden" name="ReleaseDate" value="@obj.ReleaseDate">
    <input type="hidden" name="Location" value="@obj.Location">
    <input type="hidden" name="Author" value="@obj.Author">
    </div>
</form>
<input type="file"name="fileurl" value="" id="fileurl"/>
```

### Controller
```javascript=
[HttpGet]
public ActionResult threeD(Objectmodel objectmodel, CommentModel comment)
{

    Object_f obj = new Object_f();

    //建立FTP連接
    string FTPfilePath = Server.MapPath("~/App_Start/Json/FTPconfig.json");
    StreamReader reader = new StreamReader(FTPfilePath);
    string jsonFTP = reader.ReadToEnd();
    var dataFTP = (JObject)JsonConvert.DeserializeObject(jsonFTP);
    string ftp = dataFTP["ftpserver"].Value<string>();
    string ftpUserName = dataFTP["ftpuser"].Value<string>();
    string ftpPassword = dataFTP["ftppass"].Value<string>();
    string ftpMainFolder = dataFTP["ftpMainFolder"].Value<string>();

    string Foldername = objectmodel.ReleaseDate.Replace("/", "").Replace(":", "").Replace("_", "") + "_" + objectmodel.ObjectName + "_" + objectmodel.Location;

    List<Files> ftp_objs = obj.CreateZipFile(ftp, ftpUserName, ftpPassword, ftpMainFolder, Foldername);

    if (ftp_objs.Count >= 1)
    {
        foreach(Files file in ftp_objs)
        {
            string filename = Path.GetFileName(file.FileName);
            List<string> filename_ = filename.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (!filename_.Last(   ).ToLower().Equals("ifc"))
            {
                TempData["error"] = "Your model upload wrong file type, Please change to File type(.ifc)";
                return RedirectToAction("ObjectDetail", "Object");
            }
            else
            {

                //File Download or reader: 

                // How to do??

                //---------------------
                return RedirectToAction("ObjectDetail", "Object");
            }
        }

    }

    return RedirectToAction("ObjectDetail", "Object");
}
```
存放模型檔案夾路徑：\ftp\Model\ {ReleaseDate}_ {ObjectName}_ {Location}
![](https://hackmd.io/_uploads/SkQCrG0Th.png)

{ReleaseDate}_ {ObjectName}_ {Location}裡面的資料結構：
![](https://hackmd.io/_uploads/rJi8IGCa3.png)

### Model
```javascript=
public List<string> CreateFTPList(string ftp, string ftpUserName, string ftpPassword, string ftpMainFolder, string Foldername) { 
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpMainFolder + Foldername);
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    string names = reader.ReadToEnd();
    reader.Close();
    response.Close();
    List<string> source = names.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();

    return source;
}
public List<Files> CreateZipFile(string ftp,string ftpUserName,string ftpPassword ,string ftpMainFolder,string Foldername) 
{
    List<Files> fileList = new List<Files>();
    foreach (string file in CreateFTPList(ftp, ftpUserName, ftpPassword, ftpMainFolder, Foldername))
    {
        if (file.Equals("Specification"))
        {
            string ftpFolder_t = ftpMainFolder + Foldername + "/";
            string Foldername_t = "Specification";
            foreach (string file_t in CreateFTPList(ftp, ftpUserName, ftpPassword, ftpFolder_t, Foldername_t))
            {

                    WebClient request_T = new WebClient();
                    string url = ftp + ftpFolder_t + Foldername_t + "/" +file_t;
                    request_T.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
                    byte[] bytes = request_T.DownloadData(url);
                    fileList.Add(new Files() { FileName = file_t, Bytes = bytes, gvFiles = url });

            }
        }
        else
        {

                WebClient request = new WebClient();
                string url_ = ftp + ftpMainFolder + Foldername + "/" + file;
                request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
                byte[] bytes_ = request.DownloadData(url_);
                fileList.Add(new Files() { FileName = file, Bytes = bytes_, gvFiles = url_});

        }   
    }
    return fileList;
}
```