SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<h3 data-bm="56" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: block; font: 20px / 22px Roboto, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><strong style="font-weight: 700;"> フロントエンド (HTML + JavaScript)</strong></h3><p data-bm="57" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 8px 0px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">ドラッグアンドドロップでファイルをアップロードするためのHTMLとJavaScriptを用意します。</p><p data-pasted="true"><div id="dropZone" style="border: 2px dashed #ccc; padding: 20px; text-align: center;"></p><p> ドラッグ&ドロップでファイルをアップロード</p><p></div></p><p><input type="file" id="fileInput" style="display: none;" multiple /></p><p><script></p><p> const dropZone = document.getElementById("dropZone");</p><p> const fileInput = document.getElementById("fileInput");</p><p><br></p><p> dropZone.addEventListener("dragover", (e) => {</p><p> e.preventDefault();</p><p> dropZone.style.borderColor = "blue";</p><p> });</p><p><br></p><p> dropZone.addEventListener("dragleave", () => {</p><p> dropZone.style.borderColor = "#ccc";</p><p> });</p><p><br></p><p> dropZone.addEventListener("drop", (e) => {</p><p> e.preventDefault();</p><p> dropZone.style.borderColor = "#ccc";</p><p> const files = e.dataTransfer.files;</p><p> uploadFiles(files);</p><p> });</p><p><br></p><p> function uploadFiles(files) {</p><p> const formData = new FormData();</p><p> for (let i = 0; i < files.length; i++) {</p><p> formData.append("files", files[i]);</p><p> }</p><p><br></p><p> fetch("/File/Upload", {</p><p> method: "POST",</p><p> body: formData,</p><p> })</p><p> .then(response => response.json())</p><p> .then(data => alert("アップロード成功!"))</p><p> .catch(error => alert("エラーが発生しました"));</p><p> }</p><p></script></p><p>ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー</p><h3 data-bm="59" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: block; font: 20px / 22px Roboto, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><strong style="font-weight: 700;">コントローラー (MVCアクションメソッド)</strong></h3><p data-bm="60" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 8px 0px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">アップロードされたファイルを受け取り、データベースに保存するアクションメソッドを作成します。</p><p data-pasted="true">using System;</p><p>using System.IO;</p><p>using System.Web;</p><p>using System.Web.Mvc;</p><p>using YourNamespace.Models; // データベースモデルの名前空間</p><p><br></p><p>public class FileController : Controller</p><p>{</p><p> [HttpPost]</p><p> public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)</p><p> {</p><p> if (files != null)</p><p> {</p><p> foreach (var file in files)</p><p> {</p><p> if (file != null && file.ContentLength > 0)</p><p> {</p><p> // ファイルをバイト配列に変換</p><p> using (var memoryStream = new MemoryStream())</p><p> {</p><p> file.InputStream.CopyTo(memoryStream);</p><p> byte[] fileData = memoryStream.ToArray();</p><p><br></p><p> // データベースに保存</p><p> var dbContext = new YourDbContext(); // データベースコンテキスト</p><p> var uploadedFile = new UploadedFile</p><p> {</p><p> FileName = file.FileName,</p><p> ContentType = file.ContentType,</p><p> Data = fileData,</p><p> UploadDate = DateTime.Now</p><p> };</p><p> dbContext.UploadedFiles.Add(uploadedFile);</p><p> dbContext.SaveChanges();</p><p> }</p><p> }</p><p> }</p><p> }</p><p><br></p><p> return Json(new { success = true });</p><p> }</p><p>}</p><p>ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー</p><h3 data-bm="62" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: block; font: 20px / 22px Roboto, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><strong style="font-weight: 700;">データベースモデル</strong></h3><p data-bm="63" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 8px 0px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">ファイルを保存するためのテーブルに対応するモデルを作成します。</p><p data-pasted="true">using System;</p><p><br></p><p>public class UploadedFile</p><p>{</p><p> public int Id { get; set; }</p><p> public string FileName { get; set; }</p><p> public string ContentType { get; set; }</p><p> public byte[] Data { get; set; }</p><p> public DateTime UploadDate { get; set; }</p><p>}</p><p>ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー</p><h3 data-bm="65" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: block; font: 20px / 22px Roboto, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><strong style="font-weight: 700;">データベースマイグレーション</strong></h3><p data-bm="66" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 8px 0px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">Entity Frameworkを使用している場合、マイグレーションを実行してテーブルを作成します。</p><p data-pasted="true">Add-Migration AddUploadedFileTable</p><p>Update-Database</p><p>ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー</p><h3 data-bm="68" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; display: block; font: 20px / 22px Roboto, sans-serif; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true"><strong style="font-weight: 700;">ポイント</strong></h3><ul data-bm="69" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><strong style="font-weight: 700;">ファイルサイズの制限</strong>: 大きなファイルを扱う場合、<code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">web.config</code>でアップロードサイズの制限を設定してください。</li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><strong style="font-weight: 700;">セキュリティ</strong>: アップロードされたファイルの種類を検証し、不正なファイルが保存されないように注意してください。</li></ul><p data-bm="70" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 8px 0px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: Roboto, Helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">これで、ドラッグアンドドロップでアップロードされたファイルをデータベースに登録する仕組みが完成します!</p>
コピーする
添付画像
画像を変更
Back to List
//検索文:Editor内でHTMLコードをコピーさせる