我有一个上传文件并修改然后返回的要求。我不需要将文件保存到任何位置,而是对其进行操作并返回。但是我不知道如何返回文件。
下面的代码允许保存文件,我什至写了没有保存文件的代码。唯一的问题是我收到一个错误,该文件已被其他用户打开。
该进程无法访问文件“D:\Places\places\App_Data\877d36d3-ce29-48d1-995a-ea6652a528a7C2.xlsx”,因为它正被另一个进程使用。
你能帮我么
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string path = string.Empty;
var fileName = Path.GetFileName(uploadFile.FileName);
path = Path.Combine(Server.MapPath("~/App_Data/"), Guid.NewGuid() + fileName);
uploadFile.SaveAs(path);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count;
(xlRange.Cells[4, 5] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 6] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 7] as Excel.Range).Value2 = "asdF";
(xlRange.Cells[4, 8] as Excel.Range).Value2 = "asdF";
releaseObject(xlWorksheet);
releaseObject(xlWorkbook);
releaseObject(xlApp);
GC.Collect();
uploadFile.InputStream.Dispose();
return File(path, "application/text");
}
else
{
return View();
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
//MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{enter code here
GC.Collect();
}
}