Javascript 如何上传文件路径为URL的文件

Javascript 如何上传文件路径为URL的文件,javascript,asp.net-mvc-2,Javascript,Asp.net Mvc 2,我已经编写了文件上传代码,可以很好地上传文件并将其保存在文件夹中。 我已经包括了一个功能,允许用户加载PDF文件的URL,然后从URL上传和保存文件。 守则: function loadURL(box) { var box = dhtmlx.modalbox({ title: "Load URL", text: "<div id='form_in_box'><div>Enter the URL of PDF fil

我已经编写了文件上传代码,可以很好地上传文件并将其保存在文件夹中。 我已经包括了一个功能,允许用户加载PDF文件的URL,然后从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/><div><div><textarea id='file' style='width: 400px; height: 27px;'></textarea></div><span class='dhtmlx_button'><input type='submit' value='Load URL' style='width: 86px' onclick='save_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 save_file(box) {
       var file = document.getElementById('file');
       if (file.value == "") {
           alert("Choose a file to upload");
           return false;
       }
       dhtmlx.modalbox.hide(box);

       var fd = new FormData();
       fd.append('file', file.files[0]);
       var xhr = new XMLHttpRequest();
       xhr.open('POST', '/FileUpload/Upload', true);
       xhr.onreadystatechange = function () {
           if (xhr.readyState == 4 && xhr.status == 200) {
               alert('File successfully uploaded to the server');
           }
       };
       xhr.send(fd);
函数加载URL(框){
var-box=dhtmlx.modalbox({
标题:“加载URL”,
文本:“输入PDF文件
的URL”, 宽度:“300px” }) } 函数保存_文件(框){ var file=document.getElementById('file'); 如果(file.value==“”){ 警报(“选择要上载的文件”); 返回false; } dhtmlx.modalbox.hide(box); var fd=新FormData(); fd.append('file',file.files[0]); var xhr=new XMLHttpRequest(); xhr.open('POST','/FileUpload/Upload',true); xhr.onreadystatechange=函数(){ 如果(xhr.readyState==4&&xhr.status==200){ 警报(“文件已成功上载到服务器”); } }; xhr.send(fd);
} 如果我使用上面的代码加载URL,我得到的错误如下: TypeError:file.files未定义 fd.append('file',file.files[0])

不要使用文件API(可用于从文件输入读取本地文件)。只需将URL发送到服务器并让服务器端代码获取即可。

不要使用文件API(您可以使用该API从文件输入读取本地文件)。只需将URL发送到服务器并让服务器端代码获取即可。

使用类从远程URL下载文件。您可以使用该方法从远程url下载文件

public ActionResult DownloadFile(string fileName)
{
    if (!String.IsNullOrEmpty(fileName))
    {
        using (WebClient wc = new WebClient())
        {      
            string targetPath = @"C:\YourFolder\thatUniqueFileName.pdf";         
            wc.DownloadFile(fileName,targetPath);

            return RedirectToAction("Downloaded"); //PRG pattern
        }
    }
    return VieW();
}
如果要将文件保存在项目的App_Data文件夹中,可以如下更改targetPath变量值

string targetPath = HttpContext.Server.MapPath("~/App_Data/yourPdf.pdf");
您可以解析文件url并从中获取文件名,然后向其中添加唯一标识符(以避免覆盖同名的不同文件),并使用该标识符保存文件。

使用类从远程url下载文件。您可以使用该方法从远程url下载文件

public ActionResult DownloadFile(string fileName)
{
    if (!String.IsNullOrEmpty(fileName))
    {
        using (WebClient wc = new WebClient())
        {      
            string targetPath = @"C:\YourFolder\thatUniqueFileName.pdf";         
            wc.DownloadFile(fileName,targetPath);

            return RedirectToAction("Downloaded"); //PRG pattern
        }
    }
    return VieW();
}
如果要将文件保存在项目的App_Data文件夹中,可以如下更改targetPath变量值

string targetPath = HttpContext.Server.MapPath("~/App_Data/yourPdf.pdf");
您可以解析fileUrl并从中获取文件名,然后向其中添加唯一标识符(以避免覆盖同名的不同文件),并使用该标识符保存文件