Reputation: 2901
I did browse through all the other answers but still cannot figure out what is wrong.
I am trying out a simple fileUpload
in an Azure project.
It may be that I am not closing my filestream. The following is my code:
protected void UploadButton_Click(Object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentType == "zip/rar/tar")
{
string fileName = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("~/") + fileName);
Label2.Text = "File Uploaded Successfully !";
}
else
{
Label2.Text = "File type not allowed.";
}
}
catch (Exception ex)
{
Label2.Text = "Error in File Upload. Please upload a zip/tar/rar file containing your text files.";
}
}
}
How should I close the file stream ?
Thanks Supraja
Upvotes: 0
Views: 8190
Reputation: 34780
Well, the error message says everything. The file you are trying to save as is used by "something" and therefore it's locked. Try changing the filename and it should work with no error, at least once. RD to the server if you can, and try to delete the file manually and it will tell what program is using that file. SaveAs method should not require disposal, but if there's a Close() method, call that one. Else if there is Dispose() method, call that one, after SaveAs(), and it should work well.
Upvotes: 1