6

勤務先の会社からアプリケーションの作成を依頼されたため、mvc 2010 Expressを使用してc#でプログラミングする方法を独学で学んでいます。私がしなければならない最後のことは、ユーザーがpdfファイルをアップロードおよびダウンロードできるようにすることですが、私を助ける解決策が見つかりません。

SQL Server 2005を使用しています。使用したいテーブルは ですClientsId主キーとして があります。PDFをバイナリファイルとして保存する属性を作成する必要がありますか? またはどのように?

PDFをアップロードするには、ビューにリンクがあり、ユーザーをクライアントの別のビュー(アップロードという名前)にリダイレクトしidます。コントローラ名はHomeであるため、ユーザーには次のように表示されます。

Home.aspx/Upload/2

そこで、ユーザーがアップロードしたいpdfを選択し、ボタンをクリックしてアップロードできるようにしたいと考えています。したがって、コントローラーは[HttpPost].

クライアントを編集するには、ViewModels フォルダーにモデル ビューを作成し、それらをコントローラーに渡すので、非常に簡単でした。しかし、どうすればidと pdf ファイルの両方をコントローラーに渡すことができますか? idそのユーザーが誰であるかを知る必要があります。SQL Serverのテーブルにpdfを保存するにはどうすればよいClientsですか? そして、どうすればpdfをダウンロードできますか?

4

1 に答える 1

14

これは私が使用しているものです...あなたはそれをあなたの要件に適応させるためにいくつかの変更を行う必要があります。

IDとPDFファイルの両方をコントローラーに渡すにはどうすればよいですか?

意見:

  @using (Html.BeginForm("Add", "Archivos",
                     FormMethod.Post, new { id = "attachment", enctype = "multipart/form-data", encoding = "multipart/form-data" }))
    { 


        @Html.HiddenFor(x => Model.UserID)
        <input type="file" name="uploadFile" id="uploadFile" />

       <input type="submit" value="Guardar"/>

    }

コントローラ:

   [HttpPost]
    public ActionResult Add(HttpPostedFileBase uploadFile, int UserID)
    {
        if (uploadFile != null && uploadFile.ContentLength > 0)
        {

            //instance the user.. i.e "User1"

            byte[] tempFile = new byte[uploadFile.ContentLength];
            uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);

            User1.file.Content = tempFile;
            User1.file.Save();

        }

        return RedirectToAction("Index");
    }

そして、どうすればPDFをダウンロードできますか?

コントローラ:

public ActionResult Get(int UserID)
    { 

        var User1 = new User {UserID = UserID };
        User1.LoadFile();

   //If file exists....

        MemoryStream ms = new MemoryStream(User1.File.Content, 0, 0, true, true);
        Response.ContentType = User1.File.Type;
        Response.AddHeader("content-disposition", "attachment;filename=" + User1.File.Name);
        Response.Buffer = true;
        Response.Clear();
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.End();
        return new FileStreamResult(Response.OutputStream, User1.File.Type);

    }

意見:

    @Html.ActionLink("PDF", "Get", "Files", new { UserID = Model.UserID }, new { @class = "pdf-icon-l", title="Open PDF document" })

また、SQL ServerのクライアントテーブルにPDFを保存するにはどうすればよいですか? 

データベーステーブル:

 CREATE TABLE [dbo].[FileTableName] (
 [UserID] int NOT NULL,
 [Name] varchar(256) NOT NULL,
 [Type] varchar(256) NOT NULL,
 [Length] int NOT NULL,
 [Content] varchar(max) NOT NULL) // option: varbinary(max)

ストアドプロシージャを使用しているファイルを保存するには

モデル:

 public class File
 {

    public string Name { get; set; }
    public string Type { get; set; }
    public long Length { get; set; }
    public byte[] Content { get; set; }

}

public class User

{
    public int UserID {get; set;}
    public string name {get; set;}
    /**/

    public file file {get; set;}


    /**/

    public void SaveFile()
   {
    SqlDataReader _dataReader;
    using (new MyConnectionManager())
    {
        using (_sqlCommand = new SqlCommand("SavePDFFile", MyConnectionManager.Connection)) 
       {
        _sqlCommand.CommandType = CommandType.StoredProcedure;
        _sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.UserID));
        _sqlCommand.Parameters.Add(new SqlParameter("@Name", this.Name));
        _sqlCommand.Parameters.Add(new SqlParameter("@Type", this.Type));
        _sqlCommand.Parameters.Add(new SqlParameter("@Length", this.Length));
        _sqlCommand.Parameters.Add(new SqlParameter("@Content", this.Content));
        _dataReader = _sqlCommand.ExecuteReader();

        _dataReader.Close();
    }
  }
}

  public void LoadFile()
    { 
        SqlDataReader _dataReader;
        using (new MyConnectionManager())
        {
             using (_sqlCommand = new SqlCommand("GetPDFFIle", MyConnectionManager.Connection))
            _sqlCommand.CommandType = CommandType.StoredProcedure;
            _sqlCommand.Parameters.Add(new SqlParameter("@UserID", this.IDEnsayo));
            _dataReader = _sqlCommand.ExecuteReader();
            if (_dataReader.HasRows)
            {
                _dataReader.Read();
                this.File.Name = (string)_dataReader["Name"];
                this.File.Type = (string)_dataReader["Type"];
                this.File.Length = (int)_dataReader["Length"];
                this.File.Content = (byte[])_dataReader["Content"];

            }
            _dataReader.Close();
        }
    }
  }

 }
于 2013-01-25T14:07:07.613 に答える