使用ASPX.Net和C#上传文件时调试阶段Edge浏览器意外关闭问题
问题排查与修复方案
代码层面的核心错误
你的后台代码中存在路径拼接的语法错误,这是导致调试进程终止的直接原因之一:
原代码里的路径拼接语句:
string SaveLocation = Server.MapPath("Upload") + "\" + fn;
这里的\"属于错误的转义写法,正确的转义应该用双反斜杠\\,更推荐使用Path.Combine方法(自动处理路径分隔符,避免跨平台或语法错误)。修复后的两种写法:
// 方式1:修正转义符 string SaveLocation = Server.MapPath("Upload") + "\\" + fn; // 方式2:推荐使用Path.Combine(更安全) string SaveLocation = System.IO.Path.Combine(Server.MapPath("Upload"), fn);
其他可能的问题及排查步骤
- 上传目录权限与存在性:确认项目根目录下的
Upload文件夹已创建,且调试进程拥有写入权限。可以在代码中添加自动创建目录的逻辑:string uploadDir = Server.MapPath("Upload"); if (!Directory.Exists(uploadDir)) { Directory.CreateDirectory(uploadDir); } - ASP.NET上传大小限制:默认ASP.NET会限制上传文件大小(通常为4MB),若上传文件超出此限制,会直接终止请求。可在
web.config中修改配置:<configuration> <system.web> <!-- 设置最大上传文件大小为10MB --> <httpRuntime maxRequestLength="10240" /> </system.web> <!-- IIS 7及以上版本需额外配置 --> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="10485760" /> <!-- 10MB,单位为字节 --> </requestFiltering> </security> </system.webServer> </configuration> - VS调试异常设置:检查VS的
调试 -> Windows -> 异常设置,若开启了“抛出异常时中断”的全局选项,未捕获的异常会直接终止调试进程,可根据需要调整。 - Edge浏览器兼容性:确保VS2017已安装最新补丁,以支持当前版本的Edge浏览器调试,必要时尝试以兼容模式启动Edge。
修复后的完整后台代码示例
protected void btnupload_Click1(object sender, EventArgs e) { if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0)) { string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); string uploadDir = Server.MapPath("Upload"); // 确保上传目录存在 if (!Directory.Exists(uploadDir)) { Directory.CreateDirectory(uploadDir); } string SaveLocation = System.IO.Path.Combine(uploadDir, fn); try { FileUpload1.PostedFile.SaveAs(SaveLocation); UploadStatus.Text = "文件上传成功。"; } catch (Exception ex) { UploadStatus.Text = "错误:" + ex.Message; } } else { UploadStatus.Text = "请选择要上传的文件。"; } }
内容的提问来源于stack exchange,提问作者itsmePJ
相关产品推荐
相关产品推荐

