简体   繁体   English

WebClient将图像从URL下载到服务器C#

[英]WebClient download image from URL to server C#

I have a dropbox public link that a user enters it in a textbox. 我有一个保管箱公共链接,用户可以在文本框中输入它。 Once the user clicks on a link the file should transfer to the location in the server. 用户单击链接后,文件应传输到服务器中的位置。 I ask the Dropbox forums and they mentioned that the Dropbox API is not necessary since is a public file. 我问了Dropbox论坛,他们提到Dropbox API不是必需的,因为它是一个公共文件。

https://www.dropbox.com/s/5y100bsknq7swdw/TestDoc.pdf?dl=1 https://www.dropbox.com/s/5y100bsknq7swdw/TestDoc.pdf?dl=1

Client Site 客户现场

    <div id="testarea"></div>
     <div>
     @Html.TextBoxFor(x => x.HTML_text, new { id = "tbURL" })
     <input id="btnSubmit" type="button" value="Submit" onclick="btnSubmitClick()"/>
     </div>

     <script>

      function btnSubmitClick()
      {
        $.ajax({
        url: "/Home/SaveDocument",
        datatype: "text",
        data: { 'returnUrl': $('#tbURL').val() },
        type: "POST",
        success: function (data) {
            $('#testarea').html(data);
        },
        error: function () {
            $("#testarea").html("ERROR");
        }
       });

      }
      </script>

Server Side 服务器端

        public string SaveDocument(string returnUrl)
        {
            using (var client = new WebClient())
            {
            //Not Working
              client.DownloadFile(returnUrl, "Test.pdf");
            }             
             return String.Format("Test03: String1={0};", returnUrl);
         }

The DownloadFile method will work so long as you specify a fully-qualified file name, which you have not. 只要您指定一个完全合格的文件名,就可以使用DownloadFile方法。 As an example, replacing "Test.pdf" with "C:\\\\Test.pdf" will download and save the file correctly. 例如,将"Test.pdf"替换为"C:\\\\Test.pdf"将正确下载并保存文件。 You, of course, need to make sure that the user context executing the server-side code has permission to write to the specified path. 当然,您需要确保执行服务器端代码的用户上下文具有写入指定路径的权限。

If you want to write the file to a folder relative to your application's virtual directory, you can use the Server.MapPath method to get the folder path and then concatenate your file name to the end. 如果要将文件写入相对于应用程序虚拟目录的文件夹,则可以使用Server.MapPath方法获取文件夹路径,然后将文件名连接到末尾。

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

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