SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<p data-bm="12" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; 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;" data-pasted="true">了解しました。<br>ご要望は以下の流れですね。</p><ol data-bm="13" 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;"><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Create.cshtml</code> にアクセスしたら</strong> SweetAlert2 のモーダルを表示(Yes / No)。</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;">Yes を押したら</strong> <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);"><input type="file" id="upFile"></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>を <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);"><input asp-for="CourseName"></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;"><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">/Images/</code> + ファイル名(拡張子付き)</strong> を <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);"><input asp-for="ImageUrl"></code> に設定。</li></ol><h2 data-bm="14" 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; font: 22px / 24px roboto, helvetica, sans-serif; height: 32px; -webkit-line-clamp: 1; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; 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;">実装例(jQuery + SweetAlert2)</h2><pre data-priority="2" data-bm="15" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; color: rgba(0, 0, 0, 0.8); 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; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div data-bm="20" style="display: flex; height: 28px; flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(0, 0, 0, 0.1); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(248, 244, 241);"><h5 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 0px; padding: revert; font-style: normal !important; font-variant: normal !important; font-weight: 400 !important; font-stretch: 100% !important; line-height: 18px; font-family: roboto, helvetica, sans-serif !important; font-optical-sizing: auto !important; font-size-adjust: none !important; font-kerning: auto !important; font-feature-settings: normal !important; font-variation-settings: normal !important; font-language-override: normal !important; font-size: 16px; display: inline-block; color: rgb(60, 81, 180);">Html</h5><div style="height: 28px;"><button type="button" tabindex="" title="Copy code" style="box-sizing: border-box; margin: 0px; padding: 0px; font: 400 13px / 20px roboto, helvetica, sans-serif; background-color: rgb(217, 223, 251); border: 1px solid rgba(0, 0, 0, 0); display: grid; grid-auto-flow: column; align-items: center; cursor: pointer; color: rgb(60, 81, 180) !important; text-decoration: none !important; border-radius: 9999px; block-size: 28px; gap: 2px; padding-inline: 10px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; min-inline-size: 0px; max-inline-size: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: clip; max-block-size: calc(1lh); padding-inline: 2px;">Copy code</div></button></div></div><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255); display: block; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px 16px 16px;">@* Create.cshtml *@ @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers <<span style="color: rgb(34, 134, 58);">form</span> <span style="color: rgb(0, 92, 197);">asp-action</span>=<span style="color: rgb(3, 47, 98);">"Create"</span> <span style="color: rgb(0, 92, 197);">method</span>=<span style="color: rgb(3, 47, 98);">"post"</span> <span style="color: rgb(0, 92, 197);">enctype</span>=<span style="color: rgb(3, 47, 98);">"multipart/form-data"</span>> <<span style="color: rgb(34, 134, 58);">div</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-group"</span>> <<span style="color: rgb(34, 134, 58);">label</span> <span style="color: rgb(0, 92, 197);">asp-for</span>=<span style="color: rgb(3, 47, 98);">"CourseName"</span>></<span style="color: rgb(34, 134, 58);">label</span>> <<span style="color: rgb(34, 134, 58);">input</span> <span style="color: rgb(0, 92, 197);">asp-for</span>=<span style="color: rgb(3, 47, 98);">"CourseName"</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-control"</span> <span style="color: rgb(0, 92, 197);">id</span>=<span style="color: rgb(3, 47, 98);">"CourseName"</span> /> </<span style="color: rgb(34, 134, 58);">div</span>> <<span style="color: rgb(34, 134, 58);">div</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-group"</span>> <<span style="color: rgb(34, 134, 58);">label</span> <span style="color: rgb(0, 92, 197);">asp-for</span>=<span style="color: rgb(3, 47, 98);">"ImageUrl"</span>></<span style="color: rgb(34, 134, 58);">label</span>> <<span style="color: rgb(34, 134, 58);">input</span> <span style="color: rgb(0, 92, 197);">asp-for</span>=<span style="color: rgb(3, 47, 98);">"ImageUrl"</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-control"</span> <span style="color: rgb(0, 92, 197);">id</span>=<span style="color: rgb(3, 47, 98);">"ImageUrl"</span> /> </<span style="color: rgb(34, 134, 58);">div</span>> <<span style="color: rgb(34, 134, 58);">div</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-group"</span>> <<span style="color: rgb(34, 134, 58);">label</span>>画像ファイル</<span style="color: rgb(34, 134, 58);">label</span>> <<span style="color: rgb(34, 134, 58);">input</span> <span style="color: rgb(0, 92, 197);">type</span>=<span style="color: rgb(3, 47, 98);">"file"</span> <span style="color: rgb(0, 92, 197);">id</span>=<span style="color: rgb(3, 47, 98);">"upFile"</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"form-control"</span> <span style="color: rgb(0, 92, 197);">accept</span>=<span style="color: rgb(3, 47, 98);">"image/*"</span> /> </<span style="color: rgb(34, 134, 58);">div</span>> <<span style="color: rgb(34, 134, 58);">button</span> <span style="color: rgb(0, 92, 197);">type</span>=<span style="color: rgb(3, 47, 98);">"submit"</span> <span style="color: rgb(0, 92, 197);">class</span>=<span style="color: rgb(3, 47, 98);">"btn btn-primary"</span>>登録</<span style="color: rgb(34, 134, 58);">button</span>> </<span style="color: rgb(34, 134, 58);">form</span>> <span style="color: rgb(106, 115, 125);"><!-- SweetAlert2 & jQuery --></span> <<span style="color: rgb(34, 134, 58);">script</span> <span style="color: rgb(0, 92, 197);">src</span>=<span style="color: rgb(3, 47, 98);">"https://cdn.jsdelivr.net/npm/sweetalert2@11"</span>></<span style="color: rgb(34, 134, 58);">script</span>> <<span style="color: rgb(34, 134, 58);">script</span> <span style="color: rgb(0, 92, 197);">src</span>=<span style="color: rgb(3, 47, 98);">"https://code.jquery.com/jquery-3.6.0.min.js"</span>></<span style="color: rgb(34, 134, 58);">script</span>> <<span style="color: rgb(34, 134, 58);">script</span>> $(<span style="color: rgb(215, 58, 73);">document</span>).<span style="color: rgb(111, 66, 193);">ready</span>(<span style="color: rgb(215, 58, 73);">function</span> () { <span style="color: rgb(106, 115, 125);">// ページアクセス時にモーダル表示</span> <span style="color: rgb(111, 66, 193);">Swal</span>.<span style="color: rgb(111, 66, 193);">fire</span>({ <span style="color: rgb(0, 92, 197);">title</span>: <span style="color: rgb(3, 47, 98);">'追加するアイコンを選択してください'</span>, <span style="color: rgb(0, 92, 197);">icon</span>: <span style="color: rgb(3, 47, 98);">'question'</span>, <span style="color: rgb(0, 92, 197);">showCancelButton</span>: <span style="color: rgb(0, 92, 197);">true</span>, <span style="color: rgb(0, 92, 197);">confirmButtonText</span>: <span style="color: rgb(3, 47, 98);">'Yes'</span>, <span style="color: rgb(0, 92, 197);">cancelButtonText</span>: <span style="color: rgb(3, 47, 98);">'No'</span> }).<span style="color: rgb(111, 66, 193);">then</span>((result) => { <span style="color: rgb(215, 58, 73);">if</span> (result.isConfirmed) { <span style="color: rgb(106, 115, 125);">// Yes の場合、ファイル選択ダイアログを開く</span> $(<span style="color: rgb(3, 47, 98);">'#upFile'</span>).<span style="color: rgb(111, 66, 193);">trigger</span>(<span style="color: rgb(3, 47, 98);">'click'</span>); } }); <span style="color: rgb(106, 115, 125);">// ファイル選択時の処理</span> $(<span style="color: rgb(3, 47, 98);">'#upFile'</span>).<span style="color: rgb(111, 66, 193);">on</span>(<span style="color: rgb(3, 47, 98);">'change'</span>, <span style="color: rgb(215, 58, 73);">function</span> () { <span style="color: rgb(215, 58, 73);">const</span> file = <span style="color: rgb(215, 58, 73);">this</span>.files[<span style="color: rgb(0, 92, 197);">0</span>]; <span style="color: rgb(215, 58, 73);">if</span> (!file) { <span style="color: rgb(215, 58, 73);">return</span>; <span style="color: rgb(106, 115, 125);">// ファイル未選択</span> } <span style="color: rgb(106, 115, 125);">// ファイル名と拡張子を分離</span> <span style="color: rgb(215, 58, 73);">const</span> fileName = file.name; <span style="color: rgb(215, 58, 73);">const</span> dotIndex = fileName.<span style="color: rgb(111, 66, 193);">lastIndexOf</span>(<span style="color: rgb(3, 47, 98);">'.'</span>); <span style="color: rgb(215, 58, 73);">const</span> nameWithoutExt = dotIndex > <span style="color: rgb(0, 92, 197);">0</span> ? fileName.<span style="color: rgb(111, 66, 193);">substring</span>(<span style="color: rgb(0, 92, 197);">0</span>, dotIndex) : fileName; <span style="color: rgb(106, 115, 125);">// CourseName に拡張子なしの名前を設定</span> $(<span style="color: rgb(3, 47, 98);">'#CourseName'</span>).<span style="color: rgb(111, 66, 193);">val</span>(nameWithoutExt); <span style="color: rgb(106, 115, 125);">// ImageUrl に /Images/ + ファイル名 を設定</span> $(<span style="color: rgb(3, 47, 98);">'#ImageUrl'</span>).<span style="color: rgb(111, 66, 193);">val</span>(<span style="color: rgb(3, 47, 98);">'/Images/'</span> + fileName); }); }); </<span style="color: rgb(34, 134, 58);">script</span>> </code></pre><h3 data-bm="16" 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: inline-block; font: 400 20px / 22px roboto, helvetica, 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;">補足</h3><ul data-bm="17" 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;"><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">accept="image/*"</code></strong> を付けることで画像ファイルのみ選択可能にしています。</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;">SweetAlert2 の <strong style="font-weight: 700;">Yes/No</strong> は <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">showCancelButton</code> と <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">confirmButtonText</code> / <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">cancelButtonText</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;">ファイル名の拡張子除去は <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">lastIndexOf('.')</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;">ファイル選択後に自動でフォームの入力欄に値を反映します。</li></ul><p data-bm="18" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; 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;">もしご希望であれば、このコードに <strong style="font-weight: 700;">「No を押した場合はフォーム送信を無効化」</strong> する処理も追加できますが、<br>このままでも <strong style="font-weight: 700;">Yes → ファイル選択 → 自動入力</strong> の流れは動作します。</p><p data-bm="19" style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; 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;">このコードを使えば、ページアクセス時に SweetAlert2 が出て、Yes を押すとファイル選択 → 自動入力まで完結します。<br>必要なら、次に <strong style="font-weight: 700;">サーバー側でファイル保存処理</strong> のコードもお渡しできますが、追加しますか?</p><p data-f-id="pbf" style="text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;">Powered by <a href="https://www.froala.com/wysiwyg-editor?pb=1" title="Froala Editor">Froala Editor</a></p>
コピーする
添付画像
画像を変更
Back to List
//検索文:Editor内でHTMLコードをコピーさせる