asp.net-mvc
2013-04-15 67 views 0 likes 
0

在我的應用我允許用戶從網址上傳文件如何從URL上傳文件

代碼上傳文件網址:

function loadURL(box) { 
     var box = dhtmlx.modalbox({ 
      title: "Load URL", 
      text: "<div id='form_in_box'><div>Enter the URL of PDF file <hr/><input type='text' name='files' id='files' style='width: 400px; height: 27px;'></label><br></div><div><span class='dhtmlx_button'><input type='submit' value='Load URL' style='width: 86px' onclick='load_file(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>", 
      width: "300px" 
     }) 
    } 



function load_file(box) { 
    var file = document.getElementById('files'); 
    if (file == "") { 
     alert("Enter File URL"); 
     return false; 
    } 
    file = file.value; 
    var filename = file.substring(file.lastIndexOf('/') + 1); 
    dhtmlx.modalbox.hide(box); 
    $.post("/FileUpload/UploadURL", 
        { file: '' + file + '' 
        }); 

}

控制器代碼

public ActionResult UploadURL(string file) 
    { 
     string files = Path.GetFileName(file); 
     string myStringWebResource = ""; 
     WebClient myWebClient = new WebClient(); 
     myStringWebResource = file; 
     string path = Server.MapPath(_fileUploadPath + files); 
     myWebClient.DownloadFile(myStringWebResource, path); 
     string extFile = Server.MapPath(_fileUploadPath + files); 
     return View(); 
    } 

文件已成功下載。現在我想上傳相同的文件我該怎麼做?

回答

0

HTML文件上傳控制僅適用於僅從客戶計算機上傳。你必須做的是從另一臺服務器上下載文件並保存在你的文件中。 This thread應該有所幫助。

+0

我照你說的做了。文件正在成功下載。現在我想上傳該文件並想以表格形式顯示它。我怎麼能這樣做? – pihu

+0

修改後的代碼 public ActionResult UploadURL(字符串文件) { string files = Path.GetFileName(file); string myStringWebResource =「」; WebClient myWebClient = new WebClient(); myStringWebResource = file; string path = Server.MapPath(_fileUploadPath + files); myWebClient.DownloadFile(myStringWebResource,path); string extFile = Server.MapPath(_fileUploadPath + files); – pihu

+0

@pihu請將此代碼添加到您的問題中,因爲很難從評論中讀取代碼。 –

相關問題