gpt4 book ai didi

c# - 为什么 file.PostedFile 总是为空

转载 作者:行者123 更新时间:2023-12-04 10:29:17 26 4
gpt4 key购买 nike

我需要将图像上传到我的数据库,为此我需要将它们带到服务器端。
唯一的问题是该文件始终为空,我无法使用它。

html:

<form id="signUpForm" runat="server" action="signUp.aspx" method = "get" name = "signUp" 
enctype="multipart/form-data" onsubmit = "return validateSignUp();">
<div id = "input">
<table>
<tr>
<td class = "description">*profile image: </td>
<td class = "input" id = "inputProfileImage"><input type = "file" name = "profileImage" accept = "image/png, image/jpeg, image/jpg" id = "profileImage" runat="server"/>
<div class = "warning" id = "warnProfileImage"></div>

</td>
</tr>

<tr>
<td><input type = "submit" value = "sign up"/></td>
<td><input type = "reset" value = "reset"/></td>
</tr>

</table>

</div>

<div id = "showInfo">
<table>

<tr><td class = "description">profile image:</td><td class = "input"><img src = "defaultProfileImages/defaultProfileImage1.png" id = "showProfileImage" name="showProfileImage" runat="server"/></td></tr>

<tr><td><input type = "submit" name = "submit" value = "confirm"/></td></tr>

</table>
</div>
</form>

C#:
if (Request.QueryString["submit"] != null)
{
string path = "";
if ((profileImage.PostedFile != null) && (profileImage.PostedFile.ContentLength > 0))
{
string fn = System.IO.Path.GetFileName(profileImage.PostedFile.FileName);
string SaveLocation = Server.MapPath("Temp") + "\\" + fn;
path = SaveLocation;
try
{
profileImage.PostedFile.SaveAs(SaveLocation);
Response.Write("The file has been uploaded.");
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
//Note: Exception.Message returns a detailed message that describes the current exception.
//For security reasons, we do not recommend that you return Exception.Message to end users in
//production environments. It would be better to put a generic error message.
}
}
User user = new User(Libary.convertFile(path));
UserService userService = new UserService();
userService.InsertUser(user);
Response.Redirect("homePage.aspx");
Response.End();
}

*我删除了所有与文件无关的行。

最佳答案

尝试使用 HttpPostedFileBase而不是 PostedFile快速解决问题。

关于c# - 为什么 file.PostedFile 总是为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com