2017-07-02 416 views
1

我在VB.NET中有一个代码只是为了上传ASP.NET WebForm中的文件。它在Firefox,Chrome和Safari中完美运行。但是相同的代码无法将上传的文件保存在Microsoft Internet Explorer和Microsoft Edge中,但应用程序中没有任何错误或异常。我需要某人的帮助来解决这个问题。我的.aspx代码和代码隐藏文件的代码是如下:Fileupload无法在Microsoft IE和Microsoft Edge浏览器中使用ASP.NET WebForm

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 

回答

0

这主要是因为Internet Explorer和Microsoft Edge在Chrome,Firefox和Safari中提供完整的文件路径,只提供唯一的文件名。我附上了下面的屏幕截图,并且使用Path.GetFileName方法,我只获取文件名,不管它只提供文件名或完整路径。

第一幅图像,同时从Internet Explorer和Microsoft边缘,第二个上传文件是从浏览器,Firefox和Safari

enter image description here

enter image description here

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

enter image description here

相关问题