上传到 ftp asp.net

upload to ftp asp.net(上传到 ftp asp.net)
本文介绍了上传到 ftp asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否可以使用 ASP.NET 将文件直接上传到 ftp 帐户文件夹中?

Is it possible to upload a file directly into an ftp account folder with ASP.NET ?

例如我点击浏览,选择要上传的文件,当我点击上传"按钮时,它应该将其直接保存到另一个 Web 服务器上的文件夹中,该服务器位于其他地方,而不是用于上传的服务器.

E.g. I click on browse, select a file to upload and when I click "upload" button, It should save it directly to the folder on another web server located at somewhere else other then the server that is being used to upload.

推荐答案

    /// <summary>
    /// Example call : if (FtpUpload(FileUploadControl1, "ftp.my.com/somePathDir", @"user", "pass!", "domain"))
    /// </summary>
    /// <param name="file"></param>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="ftpPass"></param>
    /// <returns></returns>
    private bool FtpUpload(FileUpload file, string ftpServer, string username, string ftpPass, string domainName = "")
    {
        // ftp://domainuser:password@ftpserver/url-path
        // If you are a member of a domain, then "ftp://domain-nameusername:password@url-path" may fail because the backslash () is sent in as a literal character and Internet Explorer incorrectly looks for a file instead of parsing a Web address. Changing the backslash () in the domain-nameusername to domainname%5Cusername works correctly.

        try
        {
            string ftpAddres;
            if (domainName != string.Empty)
                ftpAddres = "ftp://" + domainName + @"%5C" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;
            else
                ftpAddres = "ftp://" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;

            using (var webClient = new System.Net.WebClient())
            {
                webClient.UploadData(new Uri(ftpAddres), file.FileBytes);
            }

        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        return true;
    }

这篇关于上传到 ftp asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Adding Custom header to the excel file(将自定义标题添加到 excel 文件)
ASP.NET masterpages: how to insert markup in the head section inside the aspx?(ASP.NET 母版页:如何在 aspx 内的 head 部分插入标记?)
Cache-Control Headers in ASP.NET(ASP.NET 中的缓存控制标头)
Could not load file or assembly #39;Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed#39;(无法加载文件或程序集“Newtonsoft.Json,版本=4.5.0.0,文化=中性,PublicKeyToken=30ad4fe6b2a6aeed) - IT屋-程序员软件开
Specifying a custom DateTime format when serializing with Json.Net(使用 Json.Net 序列化时指定自定义 DateTime 格式)