I am using an AsyncFileUpload control inside a FormView. I only want the file to saved during the FormView's ItemInserting event. In trying to find a way to do this, the only thing I could come up with was to store the AsyncFileUpload control or the PostedFile in
a Session variable during the OnUploadedComplete() event and then invoke the SaveAs method in the ItemInserting event. Problem is the SaveAs method is throwing an exception saying "Cannot Access A Closed File". If I invoke SaveAs in the OnUploadedComplete()
event, it saves successfully, but I need it to only save when the user submits the form. Any ideas on this error or perhaps a better way to save the file during ItemInserting()?
Protected Sub frmAddDocument_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles frmAddDocument.ItemInserting
Try
Dim txtFile As AsyncFileUpload = Session("Doc")
txtFile.SaveAs(Server.MapPath("Documents") & "\" & txtFile.FileName)
Catch ex As Exception
lblAddErrorMessage.InnerHtml = ex.Message
e.Cancel() = True
End Try
End Sub
Protected Sub txtFile_OnUploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs)
Session("Doc") = sender
End Sub
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
mihixson
Member
25 Points
12 Posts
AsyncFileUpload - Cannot Access Closed File
May 12, 2010 01:40 PM|LINK
I am using an AsyncFileUpload control inside a FormView. I only want the file to saved during the FormView's ItemInserting event. In trying to find a way to do this, the only thing I could come up with was to store the AsyncFileUpload control or the PostedFile in a Session variable during the OnUploadedComplete() event and then invoke the SaveAs method in the ItemInserting event. Problem is the SaveAs method is throwing an exception saying "Cannot Access A Closed File". If I invoke SaveAs in the OnUploadedComplete() event, it saves successfully, but I need it to only save when the user submits the form. Any ideas on this error or perhaps a better way to save the file during ItemInserting()?
Protected Sub frmAddDocument_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles frmAddDocument.ItemInserting Try Dim txtFile As AsyncFileUpload = Session("Doc") txtFile.SaveAs(Server.MapPath("Documents") & "\" & txtFile.FileName) Catch ex As Exception lblAddErrorMessage.InnerHtml = ex.Message e.Cancel() = True End Try End Sub Protected Sub txtFile_OnUploadedComplete(ByVal sender As Object, ByVal e As AjaxControlToolkit.AsyncFileUploadEventArgs) Session("Doc") = sender End SubZhi-Qiang Ni...
All-Star
33491 Points
2952 Posts
Microsoft
Re: AsyncFileUpload - Cannot Access Closed File
May 17, 2010 07:42 AM|LINK
Hi mihixson,
A simply way is to save that ‘savePath’ and ‘filename’ into session, and then save it in another event:
.aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestLateSave.aspx.cs" Inherits="SoluTest_AsyncFileUpload.TestLateSave" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> <script type="text/javascript"> function pageLoad() { } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> Please upload a image:<br /> <cc1:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" UploaderStyle="Modern" ThrobberID="myThrobber" /> <asp:Label runat="server" ID="myThrobber" Style="display: none;"><img align="absmiddle" alt="" src="uploading.gif" /></asp:Label> <br /> <br /> <asp:Button ID="Button1" runat="server" Text="Save the Image" onclick="Button1_Click" /> <br /> <asp:CheckBox runat="server" ID="CheckBox1" AutoPostBack="false" Text="Check to Save and Uncheck to Do Not Save"> </asp:CheckBox><br /> Here is your image:<br /> <asp:Label runat="server" Text=" " ID="uploadResult" /><br /> <asp:Label runat="server" Text=" " ID="saveStatus" /><br /> <asp:Image ID="Image1" runat="server" /></ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>.aspx.cs file
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SoluTest_AsyncFileUpload { public partial class TestLateSave : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", "top.$get(\"" + uploadResult.ClientID + "\").innerHTML = 'Uploaded size: " + AsyncFileUpload1.FileBytes.Length.ToString() + "';", true); string savePath = MapPath("~/Uploads/" + System.IO.Path.GetFileName(e.filename)); Session["savePath"] = savePath; Session["filename"] = e.filename; if (CheckBox1.Checked) { AsyncFileUpload1.SaveAs(savePath); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "image", "top.$get(\"" + Image1.ClientID + "\").src = 'Uploads/" + System.IO.Path.GetFileName(e.filename) + "';", true); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "savestatus", "top.$get(\"" + saveStatus.ClientID + "\").innerHTML = 'The file is saved.';", true); } else { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "image", "top.$get(\"" + Image1.ClientID + "\").src = '';", true); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "savestatus", "top.$get(\"" + saveStatus.ClientID + "\").innerHTML = 'The file is not saved.';", true); } } protected void Button1_Click(object sender, EventArgs e) { AsyncFileUpload1.SaveAs(Session["savePath"].ToString()); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "image", "top.$get(\"" + Image1.ClientID + "\").src = 'Uploads/" + System.IO.Path.GetFileName(Session["filename"].ToString()) + "';", true); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "savestatus", "top.$get(\"" + saveStatus.ClientID + "\").innerHTML = 'The file is saved.';", true); } } }Have my suggestion helped?
Best regards,
Zhi-Qiang Ni
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
SAmitTyagi
Member
2 Points
1 Post
Re: AsyncFileUpload - Cannot Access Closed File
Dec 01, 2012 09:52 AM|LINK
<web>
<httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</web>
use it in your web config file.it works