8 Answers, 1 is accepted
There two ways to do that:
- Submit the form automatically using the RadAjaxManager:
<formid="form1"runat="server"><telerik:RadScriptManagerID="RadScriptManager1"runat="server"><Scripts><asp:ScriptReferenceAssembly="Telerik.Web.UI"Name="Telerik.Web.UI.Common.Core.js"/><asp:ScriptReferenceAssembly="Telerik.Web.UI"Name="Telerik.Web.UI.Common.jQuery.js"/><asp:ScriptReferenceAssembly="Telerik.Web.UI"Name="Telerik.Web.UI.Common.jQueryInclude.js"/></Scripts></telerik:RadScriptManager><scripttype="text/javascript">function fileSelected(sender, args) {$find("ajaxManager").ajaxRequest();}</script><telerik:RadAjaxManagerrunat="server"ID="ajaxManager"><AjaxSettings><telerik:AjaxSettingAjaxControlID="ajaxManager"><UpdatedControls><telerik:AjaxUpdatedControlControlID="ajaxManager"/><telerik:AjaxUpdatedControlControlID="asyncUpload"/></UpdatedControls></telerik:AjaxSetting></AjaxSettings></telerik:RadAjaxManager><telerik:RadAsyncUploadrunat="server"ID="asyncUpload"OnClientFileSelected="fileSelected"TargetFolder="~/Uploads"></telerik:RadAsyncUpload></form>
Regards,
Hristo Valyavicharski
Telerik
Hi Hristo , thank you for your quick and comprehensive reply. When I use the first method, in the page_load event I am checking this:
if ((IsPostBack) && (RadUpload1.UploadedFiles.Count > 0))
HandleUpload();
If I submit the form the Count>0 and life is good; but when the form is submitted automatically via the $find("ajaxManager").ajaxRequest() the Count is 0 and the processing doesn't fire. Should I be handling this another way?
thank you
<script type="text/javascript"> function filesUploaded(sender, args) { $find("ajaxManager").ajaxRequest("upload"); } </script> <telerik:RadAjaxManager runat="server" ID="ajaxManager" OnAjaxRequest="ajaxManager_AjaxRequest"> <AjaxSettings> <telerik:AjaxSetting AjaxControlID="ajaxManager"> <UpdatedControls> <telerik:AjaxUpdatedControl ControlID="asyncUpload" /> </UpdatedControls> </telerik:AjaxSetting> </AjaxSettings> </telerik:RadAjaxManager> <telerik:RadAsyncUpload runat="server" ID="asyncUpload" OnClientFilesUploaded="filesUploaded" TargetFolder="~/Uploads"></telerik:RadAsyncUpload>protected void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e) { if (e.Argument == "upload") { foreach (UploadedFile file in asyncUpload.UploadedFiles) { file.SaveAs(Server.MapPath("~/Uploads/") + file.FileName); } } }Regards,
Hristo Valyavicharski
Telerik
Hi Hristo, I have the same requirement to automatically complete the upload. When I tried your suggestion I got an exception when the ajaxManager_AjaxRequest method was called. The file.SaveAs() method threw a file not found exception. I ran the upload again and while looking at the temporary folder location on my machine I saw that the temporary file was removed before the call to ajaxManager_AjaxRequest was made.
I found that by simply not handling the ajaxManager_AjaxRequest method at all I got the desired results (automatic POST and completion of renaming temporary file to the target file/location).
I don't fully understand why this is, but thought I'd mention it in case others have the same trouble.
-Lou
Try to remove the
TargetFolder="~/Uploads" property.Does it help?
Regards,
Hristo Valyavicharski
Telerik
Yes, if I remove the TargetFolder property (or set it to "" on the server side) then the code in ajaxManager_AjaxRequest() runs without problems. Presumably because in that case, the temporary file doesn't automatically get renamed to the target location before the call to ajaxManager_AjaxRequest().
Could you explain why? It would be helpful to understand what is going on under the covers.
Thanks,
-Lou
protected void ajaxManager_AjaxRequest(object sender, AjaxRequestEventArgs e) { if (e.Argument == "upload") { foreach (UploadedFile file in asyncUpload.UploadedFiles) { file.SaveAs(Server.MapPath("~/Uploads/") + file.FileName); } } }is executed the file actually doesn't exits it was already saved (moved from temp to target) by the control.
Regards,
Hristo Valyavicharski
Telerik
OK - thanks for the explanation!
-Lou