asp.net - 文件上传在 ASP.NET WebForm 中的 Microsoft IE 和 Microsoft Edge 浏览器中不起作用

标签 asp.net .net vb.net webforms web-controls

I have a code in VB.NET just to upload a file in ASP.NET WebForm. It is working perfectly in Firefox, Chrome and Safari. But the same code is unable to save the uploaded file in Microsoft Internet Explorer and Microsoft Edge, although there is no any error or exception in the application. I need someone's help to fix the issue. My .aspx code and code-behind file's code are as below:

WebForm1.ASPX

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="FileUploadTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="Uploader" runat="server" />
            <asp:Button ID="cmdUpload" runat="server" Text="Upload"  />
        </div>
    </form>
</body>
</html>

代码隐藏文件

Imports System.IO

Public Class WebForm1
    Inherits Page

    Dim uploadDirectory As String = "C:\Uploads\"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub cmdUpload_Click(sender As Object, e As EventArgs) Handles cmdUpload.Click
        Dim uniqueGuid As String = Guid.NewGuid.ToString

        Dim tmpUploadDirectory As String = uploadDirectory & "\" & uniqueGuid

        If Not Directory.Exists(tmpUploadDirectory) Then
            Directory.CreateDirectory(tmpUploadDirectory)
        End If

        For Each f As HttpPostedFile In Uploader.PostedFiles
            f.SaveAs(Path.Combine(tmpUploadDirectory, f.FileName))
        Next
    End Sub
End Class

最佳答案

主要是因为 Internet Explorer 和 Microsoft Edge 给出了完整的文件路径,而在 Chrome、Firefox 和 Safari 中只给出了唯一的文件名。我附上了下面的屏幕截图,通过使用 Path.GetFileName 方法我只获取文件名,无论它只提供文件名还是完整路径。

第一张图片是从 Internet Explorer 和 Microsoft Edge 上传文件,第二张是从 Chrome、Firefox 和 Safari 上传文件

enter image description here

enter image description here

我们也可以在浏览器设置中明确启用/禁用仅文件名或完整路径。例如,下面我附上了 Internet Explorer 的屏幕截图,我们可以在其中启用或禁用“将文件上传到服务器时包含本地目录路径”。

enter image description here

关于asp.net - 文件上传在 ASP.NET WebForm 中的 Microsoft IE 和 Microsoft Edge 浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44873400/

相关文章:

html - 从 windows server 2008 升级到 2012 后,ASP.net 文件下载在文件末尾附加 html

.net - 如何更改 Excel 数据透视表的源数据

C#/ASP.NET - 从 PDF/DOC 文件获取缩略图

c# - 如何使用 $ajax 将 JSON 参数传递给 asmx web 服务

c# - 如果子节点元素相等,如何添加父节点?

c# - 如何在整个 Windows 操作系统中更改光标(图像)

asp.net - 找不到WebResources的原因

asp.net - 登录 OnClick 事件未触发

c# - 在 CodeSnippetCompileUnit 中导入命名空间

asp.net - 从客户端,强制整个页面验证