1. 程式人生 > >vs2017 瀏覽器關閉、js 程式碼 導致 自動退出除錯狀態

vs2017 瀏覽器關閉、js 程式碼 導致 自動退出除錯狀態

在 vs2017 的 工具->選項  web專案 項 取消勾選  瀏覽器視窗關閉時停止偵錯程式

而我在使用 wangEditor 富文字編輯器時,使用自定義上傳圖片的方法,由於 勾選 瀏覽器視窗關閉時停止偵錯程式 這個選項,導致請求上傳圖片 的 Controller 時就斷開除錯

js程式碼:

editor.customConfig.customUploadImg = function (files, insert) {
                // files 是 input 中選中的文件列表
                // insert 是獲取圖片 url 後,插入到編輯器的方法
                var uploadData = new FormData();
                for (var i = 0; i < files.length; i++) {
                    uploadData.append(files[i].name, files[i]);
                }


                $.ajax({
                    type: "POST",
                    url: "Upload",
                    cache: false,
                    data: uploadData,
                    processData: false, // 不做引數化處理
                    contentType: false,
                    async: false,
                    success: function (response) {
                        //alert(response);
                        console.log('success=>' + response.data);
                        for (var i = 0; i < response.data.length; i++) {
                            insert(response.data[i]);
                            console.log('insert url =>' + response.data[i]);
                        }
                    },
                    failure: function (response) {
                        //alert(response);
                        console.log('fail=>' + response.responseText);
                    },
                    complete: function (response) {
                        //alert(response);
                        console.log(response);
                    }
                });
            }

後端程式碼:

/// <summary>
        /// 圖片上傳
        /// 注入 IHostingEnvironment
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Upload([FromServices] IHostingEnvironment hostingEnvironment)
        {
            var file = Request.Form.Files[0];
            string webRootPath = hostingEnvironment.WebRootPath;// "\\wwwroot";
            var fileUrl = string.Empty;
            var filePath = string.Empty;

            if (file?.Length > 0)
            {
                // 檔名
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Guid.NewGuid().ToString() + ".jpg";
                // 存放路徑
                fileUrl = @"\upload\images";

                if (!Directory.Exists(webRootPath + fileUrl))
                {
                    Directory.CreateDirectory(webRootPath + fileUrl);
                }

                filePath = Path.Combine(fileUrl, fileName);

                using (var stream = new FileStream(webRootPath + filePath, FileMode.CreateNew))
                {
                    await file.CopyToAsync(stream);
                }
            }

            JObject jObject = new JObject();
            jObject.Add("errno", 0);
            jObject.Add("data", new JArray("https://localhost:44381" + filePath));

            return Json(jObject);
}

對於上面程式碼遇到的 除錯斷開問題 ,同樣也是 取消勾選 瀏覽器視窗關閉時停止偵錯程式 這個項就能解決

但是 為什麼會這樣,還沒想明白,有路過的大神知道,望能留下寶貴的意見,不勝感激