IFormFile沿文件名返回DriveLetter(asp.net核心)

时间:2016-10-08 10:35:31

标签: asp.net-core

我试图从IFormFile获取文件名,但它返回驱动器号:

Html Side:

<div class="small-4 columns">
   <input type="button" class="button radiusdefined alert" value="Delete" name="deletecontractfile" id="deletecontractfile" style="display:none" />
       </div>
         <div class="small-4 columns">
         <label id="selectedfilename"></label>
       </div>
         <div class="small-4 columns">
         <label for="contractfileraw" class="button">Upload</label>
         <input id="contractfileraw" name="contractfileraw" type="file" class="show-for-sr" />
</div>

JS方面:

var contractfileraw = $("#contractfileraw");

$("#deletecontractfile").on("click", function () {
    contractfileraw.replaceWith(contractfileraw = contractfileraw.clone(true));
    $('#deletecontractfile').css('display', 'none');
    $("#selectedfilename").text("");
});
$('#contractfileraw').change(function (e) {
    var fileName = e.target.files[0].name;
    $("#selectedfilename").text(fileName);
    if(fileName.length > 0){
        $('#deletecontractfile').css('display', 'block');
    }

});

和Code-Behind:

public int Insert (IFormFile contractfileraw)
        {
                if(contractfileraw.Length > 0)
                {
                    CFile file= new CFile();
                    Stream fileStream = contractfileraw.OpenReadStream();
                    byte[] buffer = new byte[16 * 1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        buffer = ms.ToArray();
                    }
                    file.FileType = contractfileraw.ContentType;
                    file.FileName = ContentDispositionHeaderValue.Parse(contractfileraw.ContentDisposition).FileName.Trim('"');
                    file.FileSize = contractfileraw.Length;
                    file.FileContent = buffer;

,结果将文件路径添加到文件名。更新到asp.net核心1后发生这种情况。

enter image description here

1 个答案:

答案 0 :(得分:2)

导入System.IO并致电Path.GetFileName(fileNameHere)。 请参阅:https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx

这适用于所有浏览器和Internet / Intranet连接。