C# 为什么是file.PostedFile总是空的
我需要上传图片到我的数据库,为了实现这一点,我需要把它们带到服务器端。 唯一的问题是文件总是空的,我不能使用它 html:C# 为什么是file.PostedFile总是空的,c#,html,asp.net,C#,Html,Asp.net,我需要上传图片到我的数据库,为了实现这一点,我需要把它们带到服务器端。 唯一的问题是文件总是空的,我不能使用它 html: *我删除了所有与文件无关的行。尝试使用HttpPostedFileBase而不是PostedFile来快速解决问题。如果在表单中使用“get”方法,则无法在服务器端获取文件。因此,您只需要将表单方法更改为“post” HTML: *Libary.save(HttpPostedFile文件,HttpServerUtility服务器)功能只是保存文件并返回其路径。检查它是否在R
*我删除了所有与文件无关的行。尝试使用
HttpPostedFileBase
而不是PostedFile
来快速解决问题。如果在表单中使用“get”方法,则无法在服务器端获取文件。因此,您只需要将表单方法更改为“post”
HTML:
*Libary.save(HttpPostedFile文件,HttpServerUtility服务器)功能只是保存文件并返回其路径。检查它是否在
Request.Files
collection中可用我很久以前使用Request.Files集合进行了尝试。没有比这里更管用的了。两者之间有什么区别?
<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>
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();
}
<form id="signUpForm" runat="server" action="signUp.aspx" method = "post" name = "signUp" enctype="multipart/form-data" onsubmit = "return validateSignUp();">
<table>
<tr>
<td class = "description">phone number: </td>
<td class = "input" id = "inputPhoneNumber"><input type = "tel" pattern="[0-9]{3}-[0-9]{7}" name = "phoneNumber" id = "phoneNumber" runat="server"/><div class = "warning" id = "warnPhoneNumber"></div></td>
</tr>
<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>
</form>
using System.Drawing;
using System.IO;
public partial class signUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ((phoneNumber.Value != null) && (phoneNumber.Value.Length > 0))
{
string path = "";
bool deleteFile = false;
if ((profileImage.PostedFile != null) && (profileImage.PostedFile.ContentLength > 0))
{
path = Libary.save(profileImage.PostedFile, Server);
deleteFile = true;
}
User user = new User(Libary.convertFile(path));
UserService userService = new UserService();
userService.InsertUser(user);
Response.Redirect("homePage.aspx");
Response.End();
if ((deleteFile) && (File.Exists(path))){File.Delete(path);}
}
}
}