首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传和上传

上传和上传
EN

Stack Overflow用户
提问于 2017-04-08 09:34:18
回答 1查看 49关注 0票数 1

我使用一个简单的代码将文件上传到我的网站,下面是我的代码:

代码语言:javascript
复制
protected void UploadFile(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Files/");

    if (!Directory.Exists(folderPath))
        Directory.CreateDirectory(folderPath);

    FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));

    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.<br/>"
       +"<br/>bytes: " + FileUpload1.FileBytes.Length
       + "<br/>Streams: "+ FileUpload1.FileContent.Length
       + "<br/>fName: " + FileUpload1.FileName;
}

FileUpload1是System.Web.UI.WebControls.FileUpload。如何通过C#代码将文件上传到我的网站?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-08 17:30:52

要上传文件,您需要使用“多部分/表单-数据”类型的POST请求。示例代码:

代码语言:javascript
复制
//create http client
using (var client = new HttpClient())
{
    //create the content we need
    using (var multipartFormDataContent = new MultipartFormDataContent())
    {
        //read the file as bytes
        var bytes = //file content

        //wrap it into the formdata
        multipartFormDataContent.Add(new ByteArrayContent(bytes));

        //do the post request and retrieve the response from the server
        var result = await client.PostAsync("myUrl.com", multipartFormDataContent);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43292410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档