[英]How to delete exist Excel file from uploaded folder?
如何关闭Excel文件或从文件夹中删除。 我尝试了很多,但没有在那里获取文件。所以总是抛出错误:该进程无法访问该文件,因为该文件正在被另一个进程使用。如何解决?
第一次没有引发任何错误.going成功上传,但是当我下次尝试使用相同的文件进行上传时,则在调用上传方法创建excel之前直接抛出错误
System.Data.DataTable dtexcel = new System.Data.DataTable();
dtexcel = BindComboWithParm("Get_Cols_Forexcelsheet");
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dtexcel, "Customer");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Customer_Creation.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
检查文件
string FileName = "Customer_Creation";
string Paths = Server.MapPath("~/Uploads/") + FileName;
FileInfo file = new FileInfo(Paths);
if (file.Exists)
{
file.Delete();
}
上传活动点击
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string FileName = "Customer_Creation";
string Paths = Server.MapPath("~/Uploads/") + FileName;
FileInfo file = new FileInfo(Paths);
if (file.Exists)
{
file.Delete();
}
if (FileUpload1.HasFile)
{
string excelPath = Server.MapPath("~/Uploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(excelPath);
ImporttoSQL(excelPath);
}
else
{
ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "ClientScript", "alert('Please select Excelsheet')", true);
return;
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
}
finally
{
ViewState["ExcelUploaded"] = "false";
}
}
I believe you just want to create a file, download it and then delete it once it has downloaded.
1. Create a custom FileHttpResponseMessage.
public class FileHttpResponseMessage : HttpResponseMessage
{
private readonly string filePath;
public FileHttpResponseMessage(string filePath)
{
this.filePath = filePath;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Content.Dispose();
if(File.Exist(filePath))
File.Delete(filePath);
}
}
2. Create a function which will return generated file path. and use that path in below code :
public HttpResponseMessage Get()
{
var filePath = GetNewFilePath();//your function which will create new file.
var response = new FileHttpResponseMessage(filePath);
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName ="YourCustomFileName"
};
return response;
}
3. Above code will delete file automatically once file will be served to user.
现在很难说出什么问题了。 程序的某些部分很可能仍在使用文件。 请检查此链接 ,其中包含有关如何调试的有用信息。
您的应用已将文件上传到服务器。 为此,它使用了托管资源(如FileStream等)。 由于某种原因,该文件保持打开状态。 稍后,您的应用程序仍在使用时尝试将其删除。 您会收到此“文件正在使用”异常。
我建议做的是尝试直接删除此文件,如果可行,则问题隐藏在代码的“上传部分”中。 如果不是,那么问题很可能出在使用此文件的某些外部进程上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.