サーバーに画像をアップロードするためにこのコードを使用します。はい、動作します。しかし、データベースに画像パスを送信する必要があります。プロファイル テーブルと ImagePath フィールドを持つデータベースがあります。
これどうやってするの...
私の作業コードはこちら
public partial class ProfileDetails2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void upload_Click(object sender, EventArgs e)
{
int intFileSizeLimit = 10;
string strFileNameWithPath = FileUpload1.PostedFile.FileName;
string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
int intFileSize = FileUpload1.PostedFile.ContentLength / 1024;
strExtensionName = strExtensionName.ToLower();
if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif"))
{
if (intFileSize < intFileSizeLimit)
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/images/") + "img" + strExtensionName);
lblMessage.Text = "Uploaded file details <hr />" +
"File path on your Computer: " + strFileNameWithPath + "<br />" +
"File Name: " + strFileName + "<br />" +
"File Extension Name: " + strExtensionName + "<br />" +
"File Size: " + intFileSize.ToString();
}
else
{
lblMessage.Text = "File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
}
}
else
{
lblMessage.Text = "Only .jpg or .gif file are allowed, try again!";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
}