简体   繁体   English

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

[英]How to upload file where filepath is URL

I have written file upload code that work fine for uploading a file and saving it in a folder. 我编写了文件上传代码,可以很好地用于上传文件并将其保存在文件夹中。 I have included a functionality that allows the user to load URL of the PDF file and than the file from URL should be uploaded and saved. 我已经包含了一项功能,该功能允许用户加载PDF文件的URL,然后应上传和保存URL中的文件。 The code: 编码:

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);

} If i use the above code for load_URL am getting error as: TypeError: file.files is undefined fd.append('file', file.files[0]); }如果我对load_URL使用以上代码,则会报错:TypeError:file.files未定义fd.append('file',file.files [0]);

Don't use the Files API (which you can use for reading a local file from a file input). 不要使用Files API(可用于从文件输入中读取本地文件)。 Just send the URL to the server and have your server side code fetch it. 只需将URL发送到服务器,并让您的服务器端代码来获取它即可。

Use WebClient class to download the file from the remote url. 使用WebClient类从远程URL下载文件。 You can use the DownloadFile method to download a file from a remote url. 您可以使用DownloadFile方法从远程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();
}

If you want to save the files in your App_Data folder of the project, you can change the targetPath variables value like this 如果要将文件保存在项目的App_Data文件夹中,则可以像这样更改targetPath变量值

string targetPath = HttpContext.Server.MapPath("~/App_Data/yourPdf.pdf");

You could parse the fileUrl and get the file name from that and add a unique identifier to that(to avoid overwriting of different files with same name) and use that to save the files. 您可以解析f​​ileUrl并从中获取文件名,然后向其添加唯一标识符(以避免覆盖具有相同名称的不同文件),然后使用该标识符来保存文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM