简体   繁体   English

将文件从C#发布到ASP.Net

[英]Posting a file from C# to ASP.Net

I have a C# client which once every hour needs to post some zip files to ASP.Net site. 我有一个C#客户端,每小时一次需要将一些zip文件发布到ASP.Net站点。 This needs to be completely automated with no user interaction. 这需要完全自动化,无需用户交互。

Wondering the best way to go about it. 想知道最好的方法。

Ideally would like to post the file without setting up any non .aspx / .asp pages. 理想情况下,想要发布文件而不设置任何非.aspx / .asp页面。

Thanks for the help! 谢谢您的帮助!

It depends on what the target site expects as content type. 它取决于目标站点期望的内容类型。 If it is multipart/form-data then a simple WebClient should do the job: 如果它是multipart/form-data那么一个简单的WebClient应该完成这项工作:

using (var client = new WebClient())
{
    byte[] result = client.UploadFile(
        "http://foo.com/index.aspx", @"d:\foo\bar.zip"
    );
    // TODO: Handle the server response if necessary
}

Send a HttpRequest containing all the necessary information including the bytes of the file. 发送包含所有必要信息的HttpRequest,包括文件的字节。 Google should help you on this one. 谷歌应该帮助你。

Nevertheless, I don't understand why you don't want to use a non .aspx page for this. 不过,我不明白你为什么不想使用非.aspx页面。 A generic handle ( .ashx) is suitable for this. 通用句柄( .ashx)适用于此。 But I still suggest you use another way to upload that file, eg per FTP and use a service that watches the directoy with a FileWatcher to determine and act on changes 但是我仍然建议你使用另一种方式来上传该文件,例如每个FTP并使用一个服务,该服务使用FileWatcher监视directoy以确定并根据更改采取行动

为了自动执行任务,您可以使用DispatcherTimer(http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx),为Tick事件分配处理程序。

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

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