0

参照ボタンがあり、それをデータベースに保存しています。

mvc3 の使用

見る

 @using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = multipart/form-data" }))
{
<input type="file" name="file" />
}

コントロール

public ActionResult Upload(FormCollection collection HttpPostedFileBase file)
      string filefullpath = Path.GetFullPath(file.FileName);

Path.GetFullPath はファイル名だけを返し、コードがクラッシュすると、完全なファイル パスが必要になります。

テストのフルパス C:\filename.jpg を渡してテストしてみましたが、うまくいきました。そのため、関数に渡されるフルパスがありません....

ユーザーが選択した参照ボタンからフル パスを取得するにはどうすればよいですか。

ありがとう、

4

1 に答える 1

0

私の知る限り、あなたはフルパスを得ることができません。それはセキュリティと関係があります。

とにかく、あなたはあなたが好きなところにストリームを保存するので、あなたはそれを必要としません。

アップデート:

いくつかの最低限のコード:

var file = Request.Files[0];

if (file == null || file.ContentLength == 0)
{
    throw new InvalidOperationException("No file has been selected for upload or the file is empty.");
}

file.SaveAs(path);  // <-- path is somewhere on you *local* drive

またはデータベースに:

データ構造

CREATE TABLE [dbo].[SystemImage](
[Id] [uniqueidentifier] NOT NULL,
[ImageData] [image] NOT NULL,
CONSTRAINT [PK_SystemImage] PRIMARY KEY CLUSTERED 
(
[Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[SystemImage] ADD  CONSTRAINT [DF_SystemImage_Id]  DEFAULT (newid()) FOR [Id]
GO

コード

using (var connection = new SqlConnection(@"data source=.\SQLEXPRESS;initial catalog=ImageTest;Integrated Security=SSPI;"))
{
    connection.Open();

    using (var command = connection.CreateCommand())
    {
        command.CommandType = CommandType.Text;
        command.CommandText = "insert into SystemImage (ImageData) values (@ImageData)";

        command.Parameters.AddWithValue("ImageData", file.InputStream.ToBytes());
        command.ExecuteNonQuery();
    }
}

Stream.ToBytes()拡張方法については、次のリンクを参照してください。

http://shuttle.codeplex.com/SourceControl/changeset/view/3f7f1f2cf2c0#Shuttle.Core%2fsource%2fShuttle.Core.Infrastructure%2fExtensions%2fStreamExtensions.cs

于 2012-04-16T15:32:44.160 に答える