ASP.NETのWebアプリを初めて使用しましたが、この問題に遭遇しました。
以前にSharePointページに保存されていたtemplate.xlsをダウンロードするためのボタンがあるページがあります。ダウンロード方法がありますが、問題なく動作しています。問題のテンプレートは、ローカルIISのWebアプリが配置されているフォルダーの本来ある場所に保存されています。問題は、このファイルをエンドユーザーに公開することです。ユーザーにポップアップを表示して、ユーザーがこのtemplate.xlsを開いたり保存したりできるようにする必要があります。私が使用しているのは次のとおりです。
//path on my local IIS where the file is located after the download
string _strFolderApp = "~/Arq/";
string _strFullFolderApp = _strFolderApp + _strFileName;
string apPath = System.Web.Hosting.HostingEnvironment.MapPath(_strFullFolderApp);
FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
// Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(_contentFile, 0, _contentFile.Length);
//opens the file
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
Response.ContentType = "";
Response.TransmitFile(apPath);
Response.Flush();
File.Delete(apPath);
Response.End();
# endregion
// close file stream
_FileStream.Close();
オンラインで検索したところ、すべての回答がFileShare.ReadWriteを使用することになり、両方のプロセスが正しく機能するようになりました。しかし、コードがResponse.TransmitFile(apPath);に到達すると、それは機能しません。例外が発生し、ポップアップが表示されません。例外 :
別のプロセスによって使用されているため、プロセスはファイル'c:\ inetpub \ wwwroot \ MyWebApp \ File\TemplateSharePoint.xlsx'にアクセスできません。
助けていただければ幸いです:)
コードの更新を編集
if (_flgSPSDownload)
{
System.IO.FileStream _FileStream = new System.IO.FileStream(apPath,System.IO.FileMode.Create, System.IO.FileAccess.Write);
_FileStream.Write(_contentFile, 0, _contentFile.Length);
_FileStream.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
//Set the appropriate ContentType.
Response.ContentType = "Application/x-msexcel";
Response.TransmitFile(apPath);
Response.Flush();
//Write the file directly to the HTTP content output stream.
Response.WriteFile(apPath);
//File.Delete(apPath);
//Process.Start(_strDestinationPath + "//" + _strFileName);