[英]File upload using multipart/form-data
我使用此代码上传文件,但未上传
var content = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(filePath));
fileContent.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment")
{
FileName = Path.GetFileName(filePath)
};
content.Add(fileContent);
var responce = client.PostAsync(queryString.ToString(), content).Result;
尝试这个
<form id="form1" runat="server">
<asp:FileUpload id="FileUploadControl" runat="server" />
<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" /> <br /><br />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>
protected void UploadButton_Click(object sender, EventArgs e)
{
if(FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch(Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
状态消息应清楚表明它们的全部含义,您可以根据需要进行更改。
试试这个代码。 它的工作正常。 只需要在解决方案资源管理器中创建一个名为“ Upload”的文件夹,即可上传文件。
在您的aspx页面中
<asp:FileUpload runat="server" ID="FileUploadContacts" Width="300px" />
<asp:Button runat="server" ID="btnUpload" Text="Upload" OnClick="btnUpload_OnClick" />
在后面的代码中执行此操作
protected void btnUpload_OnClick(object sender, EventArgs e)
{
try
{
if (FileUploadContacts.HasFile)
{
FileUploadContacts.SaveAs(Server.MapPath("Uploads//") + FileUploadContacts.FileName);
this.lblMessage.Text = "File uploaded Successfully!>";
}
else
{
this.lblMessage.Text = "File not uploaded!>";
}
}
catch (Exception ex)
{
Logger.WriteException(ex);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.