开发者

Upload file to aspx site with WebClient

开发者 https://www.devze.com 2023-02-21 02:58 出处:网络
I have an aspx website and I\'d like to upload a file to it. The file is being processed later, so I need to support some extra information with it.

I have an aspx website and I'd like to upload a file to it. The file is being processed later, so I need to support some extra information with it.

This is the website I have to far:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="ABC.Web.Upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="Form1" method="post" enctype="multipart/form-data" runat="server">
    <input type="file" id="File1" name="File1" runat="server" />
    <input type="text" id="FileId" name="FileId" runat="server" />
    <input type="submit" id="Submit1" value="Upload" runat="server" />
    </form>
</body>
</html>

This (and the code behind) works very well when using a browser and clicking through the form. But I want to upload the file using the WebClient from C# code. I don't have to use the WebClient, so I am open to other suggestions. I can also modify the upload.aspx to suit me, there is no requirement that it works from a browser, but this is the only example I've got working.

Looking at fiddler (I just started using it, so I probably have missed it) I could not figure out how I would create such a request from code.

At the moment when using the WebClient I am specifying the address and the local filename. I have a breakpoint in the code behind in my website but the File1 and FileId are not set (as expected).

How do I have to modify the website and how开发者_如何学运维 do I have to use the WebClient to get the upload working?

EDIT: Just found out about the Request.Files, that will give me the file, but how about the other fields?


Many ways to do this the easiest would be to set File1 and Field as part of the query string in your WebClient. Then on the server side you should have these values in the Request object.

myQueryStringCollection.Add("File1",yourFile1);
myQueryStringCollection.Add("FileId",myFileId); 
myWebClient.QueryString = myQueryStringCollection;

Note, don't do this if you are trying to keep this information secure and are using ssl. In which case add it to the form data being posted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号