简体   繁体   中英

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. 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.

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:

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. 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. A generic handle ( .ashx) is suitable for this. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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