【问题标题】:Don't get right uploaded file name in IE8 (asp.net mvc) [duplicate]不要在 IE8 中获取正确的上传文件名(asp.net mvc)[重复]
【发布时间】:2013-07-17 17:07:17
【问题描述】:

在我的 MVC 项目中,我有一个上传文件的表单。如果我使用 Google Chrome、Firefox 或 Opera 上传文件,我只会得到像 Inventory_June_2013.xlsx 这样的文件名。

当我使用 IE8 上传文件时,我得到一个类似的文件名 C:\Documents and Settings\gornel\My Documents\Inventory_June_2013.xlsx.

如何解决这个问题?

UPD
这是我的 File.cs

using System;
using System.ComponentModel.DataAnnotations;
namespace Argussite.SupplierService.Core.Domain
{
public class File : Entity
{
    public const int ContentTypeLength = 100;
    public const int FileNameLength = 100;
    public const int StorageNameLength = 100;

    protected File()
    {}

    public File(string name, string contentType, long fileSize)
    {
        Name = name;
        ContentType = contentType;
        FileSize = fileSize;
        StorageName = Guid.NewGuid().ToString("D");
        UploadTime = DateTime.Now;
    }

    [Required, MaxLength(FileNameLength)]
    public string Name { get; set; }

    [Required, MaxLength(ContentTypeLength)]
    public string ContentType { get; set; }

    public long FileSize { get; set; }

    [Required, MaxLength(StorageNameLength)]
    public string StorageName { get; set; }

    public DateTime UploadTime { get; set; }
}
}

这是来自控制器的代码

public ActionResult UploadFile(Guid eventId, HttpPostedFileBase file)
    {
        //...
        var document = new File(file.FileName, file.ContentType, file.ContentLength);
        @event.FileId = document.Id;
        @event.ActualDate = document.UploadTime;

        Context.Files.Add(document);

        file.SaveAs(GetFilePath(document.StorageName));

        Register(new DocumentUploadedNotification(@event, @event.DocumentType, document, UrlBuilder));

        return RedirectToAction("Details", "Suppliers", new { id = @event.SupplierId });
    }

我使用类 HttpPostedFileBase 和属性 FileName。

【问题讨论】:

  • 你能显示处理上传文件的代码吗?
  • 我更新了我的问题。
  • 那么所有这些代码中哪个变量的值是错误的?
  • 请删除不必要的代码,这对问题没有价值。
  • 现在更清楚了,在这种情况下,HttpPostedFile.FileName - Different from IE 会回答您的问题。

标签: asp.net-mvc-4


【解决方案1】:

众所周知,IE 返回完整路径,而其他浏览器仅提供文件名。不幸的是,这是您必须自己处理的情况,asp.net无法提供帮助。

您可以使用Path.GetFileName(file.FileName) 方法仅获取文件名部分。

【讨论】:

  • 我用这个代码var document = new File(file.FileName, file.ContentType, file.ContentLength);应该怎么改?
  • 请使用此代码更新您的问题。并显示file 是什么
  • 我更新了我的问题。
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 2017-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
相关资源
最近更新 更多