0

数時間前に MVC4 ビジュアル スタジオで作業しています。これは本当に新しいので、誰かが私を助けてくれることを願っています。

私の Web サイトには、input type="text" と input type="file" のフォームがあります。

複数の画像アップロードを使用して、データベースに新しいアイテムを挿入したいと考えています。

ビュー、コントローラー、モデルでこれを実行しようとしています。

何かを検索して、いくつかの画像のアップロードに役立つコードを見つけました。

しかし、ボタンをクリックすると、すべての画像がアップロードされ、他のフィールドからのすべての情報とすべての画像のファイル名がテーブルに挿入されます。したがって、この ID に関連付けられているすべての画像を別のページで選択できます。

誰かがこれを手伝ってくれることを願っています。

ここに私のコードがあります:

前もって感謝します

意見:

@using (Html.BeginForm("Uploading", "AdicionarRevista", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

<label for="file">Nome da Empresa:</label>   
<input type="text" name="Name" id="nome" /><br />

<label for="file">Titulo da Revista:</label>       
<input type="text" name="Title" id="titulo" /> <br />

<label for="file">Upload:</label>         
<input type="file" name="ImageUploaded" id="btnUpload" multiple="multiple" accept="image/*"  />    <br />

<button type="submit"  id="Upload">Upload</button>
}

コントローラ:

  [HttpPost]
        public ActionResult Uploading(ImageModel infouploadimage)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (Request.Files[i].ContentLength > 0)
                {
                    HttpPostedFileBase uploadedFile = Request.Files[i];
                    string fileName = Guid.NewGuid().ToString();
                    string serverPath = Server.MapPath("~");
                    string imagesPath = serverPath + "Content\\Images\\";
                    string thumsise = Path.Combine(imagesPath, "Thumb" + fileName);
                    string thumbPath = Path.Combine(imagesPath, "Thu" + fileName);
                    string fullPath = Path.Combine(imagesPath, "Full" + fileName);
                    string Bigpath = Path.Combine(imagesPath, "big" + fileName);
                    string Bigpatha = Path.Combine(imagesPath, "biga" + fileName);
                    string Bigpathb = Path.Combine(imagesPath, "bigb" + fileName);
                    string Bigpathc = Path.Combine(imagesPath, "bigc" + fileName);
                    ImageModel.ResizeAndSave(thumsise, fileName, uploadedFile.InputStream, 80, true);
                    ImageModel.ResizeAndSave(thumbPath, fileName, uploadedFile.InputStream, 100, true);
                    ImageModel.ResizeAndSave(fullPath, fileName,  uploadedFile.InputStream, 500, true);
                    ImageModel.ResizeAndSave(Bigpath, fileName, uploadedFile.InputStream, 200, true);
                    ImageModel.ResizeAndSave(Bigpatha, fileName, uploadedFile.InputStream, 250, true);
                    ImageModel.ResizeAndSave(Bigpathb, fileName, uploadedFile.InputStream, 150, true);
                    ImageModel.ResizeAndSave(Bigpathc, fileName, uploadedFile.InputStream, 50, true);

                }
            }


            return RedirectToAction("Index", "Home");
        }

モデル:

 public class ImageModel
    {

        [Required]

        public string Name{ get; set; }
        public string Title{ get; set; }

        public HttpPostedFileWrapper ImageUploaded { get; set; }
        public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
        {
            int newWidth;
            int newHeight;
            Image image = Image.FromStream(imageBuffer);
            int oldWidth = image.Width;
            int oldHeight = image.Height;
            Bitmap newImage;
            if (makeItSquare)
            {
                int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
                double coeficient = maxSideSize / (double)smallerSide;
                newWidth = Convert.ToInt32(coeficient * oldWidth);
                newHeight = Convert.ToInt32(coeficient * oldHeight);
                Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
                int cropX = (newWidth - maxSideSize) / 2;
                int cropY = (newHeight - maxSideSize) / 2;
                newImage = new Bitmap(maxSideSize, maxSideSize);
                Graphics tempGraphic = Graphics.FromImage(newImage);
                tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
                tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
            }
            else
            {
                int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;

                if (maxSide > maxSideSize)
                {
                    double coeficient = maxSideSize / (double)maxSide;
                    newWidth = Convert.ToInt32(coeficient * oldWidth);
                    newHeight = Convert.ToInt32(coeficient * oldHeight);
                }
                else
                {
                    newWidth = oldWidth;
                    newHeight = oldHeight;
                }
                newImage = new Bitmap(image, newWidth, newHeight);
            }
            newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            //newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
            image.Dispose();
            newImage.Dispose();
        }

        public void infouploadimage(string name, string title,string filename)
        {
            Name= name;
            Title= title;
           ImageUploaded = filename; <== ERROR HERE ALSO

            string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection connect = new SqlConnection(connection))
            {
                string query = "Insert Into MYtable(Name, Title, Images) Values(@name, @title, ???)";

                SqlCommand command = new SqlCommand(query, connect);
                command.Parameters.AddWithValue("NomeEmpresa", n_empresa);
                command.Parameters.AddWithValue("Titulo", titulo_revista);
                //command.Parameters.AddWithValue("filename", ImageUploaded);
                connect.Open();
                command.ExecuteNonQuery();
            }
        }
}
4

1 に答える 1

0

まず、コントローラーの POST メソッド内にブレークポイントを設定します。デバッグ中に、実行がこのブレークポイントに到達し、モデルのすべてのプロパティが期待どおりに設定されていることを確認します。そうでない場合は、フォームをモデルにバインドする際に問題が発生する可能性があります。

また、SQL プロファイラーに精通している場合は、これを使用して、SQL 挿入が実際に行われているかどうか、値が渡されているかどうかを確認できます。

コードのデバッグ能力を向上させる方法を検討することをお勧めします。おそらく、いくつかの追加のログも同様です。

メソッドの移動に関するコメントについては、ドメイン モデル パターン/ドメイン駆動設計を調べてください。また、アンチパターンの「Anemic Domain Model」(これに眉をひそめる人もいます) を調べて、メソッドがどこに行く必要があるかについて自分で決定できるようにします。

于 2013-07-29T17:47:31.300 に答える