另一个进程在使用文件html元素时使用的文件

本文关键字:文件 元素 html 另一个 进程 | 更新日期: 2023-09-27 18:10:24

我正在开发一个使用Azure blob存储的文件上传弹出窗口。弹出窗口的"肉"看起来像这样

<div id="bookmarkFileSelector" style="display: none">
    <div>
       <div class="demo-section">
          <div id="bookmarkListView"></div>
          <div id="bookmarkPager" class="k-pager-wrap"></div>
       </div>
       <button type="button" class="btn btn-info" ng-click="">Select</button>
       <button type="button" class="btn btn-info" id="uploadFilesButton" ng-click="openFileDialog()">Upload</button>
       <button type="button" class="btn btn-info"
               ng-disabled="isBookmarkSelected == false" id="deleteFileButton"
               ng-click="deleteFile()" style="margin-left: 20px">Delete</button>
       <div id="uploadFiles" >
          <input type="file" id="selectedFile" />
       </div>
   </div>
</div>

当我点击Upload按钮时,我调用这个函数

$scope.openFileDialog = function () {
    $('#selectedFile').focus();
    $('#selectedFile').click();
};

这样我打开文件对话框,但不必使用默认布局,但我的(bootstrap)按钮。

当我选择一个文件后,这个函数将根据改变的事件

被调用
$('#selectedFile:file').change(function () {
    var filePath = $('#selectedFile').val();
    var fileName = this.files[0].name;

    $scope.uploadFile({
        'filePath': filePath,
        'fileName': fileName
    });
});

然后依次获取文件名和文件路径并将其发送给ASP。. NET MVC控制器函数

[Authorize]
[HttpPost]
public ContentResult UploadFile(string filePath, string fileName)
{
    using (var fileStream = System.IO.File.Open(filePath, FileMode.Open))
    {
        //Do some stuff with the file
    }
}

但是当我想打开文件时,我得到了这个异常:

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not
handled in user code
Additional information: The process cannot access the file 'foo' because it is being
used by another process.

我猜</file>仍然持有文件,所以我不能在c#中打开它。

我需要做些什么来"清除"或从文件选择器中释放文件才能上传它?

编辑:

我也可以通过其他方式来获取所选文件的路径

另一个进程在使用文件html元素时使用的文件

现在回答有点晚了,但希望能对别人有所帮助。我最近一直在处理同样的问题。以下是我发现并适用于我的方法。当您打开文件时,添加这些附加属性以允许您访问。

FileAccess。读,文件共享。读

:

using (FileStream theStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))

希望这对你有帮助。欢呼,