无法使用 C# 在 asp.net 中从客户端接收文件
Can't receive files from client side in asp.net using C#
我正在尝试从表单中获取个人资料图片。 html 看起来像这样:
<form action="UpdateProfile.aspx" method="post">
<input type='file' name='profilePic'/>
</form>
服务器端代码如下所示:
HttpPostedFile image = Request.Files["profilePic"];
string imagePath = @"../Pictures/Profile/DefaultPicture.png";
if (image.ContentLength >0 && image != null)
{
if (Path.GetExtension(image.FileName) == "jpg" || Path.GetExtension(image.FileName) == "jpeg" ||
Path.GetExtension(image.FileName) == "png" || Path.GetExtension(image.FileName) == "gif" ||
Path.GetExtension(image.FileName) == "bmp")
{
string fileExtention = Path.GetExtension(image.FileName);
string FileName = Session["Username"].ToString() + "." + fileExtention;
image.SaveAs(Server.MapPath(Path.Combine("../Pictures/Profile/", FileName)));
imagePath = Path.Combine("../Pictures/Profile/", FileName);
}
}
但它永远不会起作用,无论我上传什么 image
变量总是 null
。
它就像服务器无法接收它或其他东西。你们能帮我解决这个问题还是告诉我哪里出错了?先感谢您。
一个指导你的例子。
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
using System.IO;
using System.Collections.Generic;
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
确保您正确设置了 Content-Type:
The file collection is populated only when the HTTP request
Content-Type value is "multipart/form-data".
https://msdn.microsoft.com/en-us/library/system.web.httprequest.files%28v=vs.110%29.aspx
这是一个例子:
<FORM action="..."
enctype="multipart/form-data"
method="post">
...
</FORM>
我正在尝试从表单中获取个人资料图片。 html 看起来像这样:
<form action="UpdateProfile.aspx" method="post">
<input type='file' name='profilePic'/>
</form>
服务器端代码如下所示:
HttpPostedFile image = Request.Files["profilePic"];
string imagePath = @"../Pictures/Profile/DefaultPicture.png";
if (image.ContentLength >0 && image != null)
{
if (Path.GetExtension(image.FileName) == "jpg" || Path.GetExtension(image.FileName) == "jpeg" ||
Path.GetExtension(image.FileName) == "png" || Path.GetExtension(image.FileName) == "gif" ||
Path.GetExtension(image.FileName) == "bmp")
{
string fileExtention = Path.GetExtension(image.FileName);
string FileName = Session["Username"].ToString() + "." + fileExtention;
image.SaveAs(Server.MapPath(Path.Combine("../Pictures/Profile/", FileName)));
imagePath = Path.Combine("../Pictures/Profile/", FileName);
}
}
但它永远不会起作用,无论我上传什么 image
变量总是 null
。
它就像服务器无法接收它或其他东西。你们能帮我解决这个问题还是告诉我哪里出错了?先感谢您。
一个指导你的例子。
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
using System.IO;
using System.Collections.Generic;
protected void Upload(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
确保您正确设置了 Content-Type:
The file collection is populated only when the HTTP request Content-Type value is "multipart/form-data".
https://msdn.microsoft.com/en-us/library/system.web.httprequest.files%28v=vs.110%29.aspx
这是一个例子:
<FORM action="..."
enctype="multipart/form-data"
method="post">
...
</FORM>