0

サーバーに画像をアップロードするためにこのコードを使用します。はい、動作します。しかし、データベースに画像パスを送信する必要があります。プロファイル テーブルと 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;
    }
}

}

4

1 に答える 1

0

を使用してパスを追加できます

string strFileNameWithPath = Request.PhysicalApplicationPath + "Uploads\" + FileUpload1.PostedFile.FileName;

これにより、アプリケーションのルート パス内の完全なパスが得られます。

これがあなたが探しているものだと思います。

于 2012-06-29T18:29:37.420 に答える