我使用此代码上传文件但未上传
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;
答案 0 :(得分:0)
试试这个
<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;
}
}
}
状态信息应清楚地表明它们的全部内容,您可以根据自己的需要进行更改。
答案 1 :(得分:0)
试试这段代码。它工作正常。只有您必须在解决方案资源管理器中创建一个名为“上传”的文件夹,其中将上载文件。
在您的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);
}
}