0

In my MVC project I have a form to upload a file. If I use Google Chrome, Firefox or Opera to upload a file, I get only the file name like Inventory_June_2013.xlsx.

When I use IE8 to upload a file, I get a file name like C:\Documents and Settings\gornel\My Documents\Inventory_June_2013.xlsx.

How to fix this?

UPD
This is my 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; }
}
}

And it's code from controller

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 });
    }

I use class HttpPostedFileBase and property FileName.

Heidel
  • 3,174
  • 16
  • 52
  • 84

1 Answers1

2

It's known that IE returns full path while other browsers supply only file name. Unfortunately, it's the case which you have to handle by yourself, asp.net cannot help with it.

You may take only file name part using Path.GetFileName(file.FileName) method.

Dima
  • 6,721
  • 4
  • 24
  • 43
  • I use this code `var document = new File(file.FileName, file.ContentType, file.ContentLength);` How should I change it? – Heidel Jul 17 '13 at 08:18
  • 1
    Please update your question with this code. And show what `file` is – Dima Jul 17 '13 at 08:19