This is a migrated thread and some comments may be shown as answers.

Simple submit button that saves to a specified folder

4 Answers 529 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 05 Jun 2017, 08:09 PM

Hello, 

 

I am just having some issues creating a simple submit button that will save an uploaded file to a specified folder upon the clicking of the button. 

 

If anyone could assist me in pointing me the right direction or how you could go about doing this, it would be greatly appreciated. 

 

 <telerik:radasyncupload runat="server" CssClass="async-attachment" ID="AsyncUpload1"
                HideFileInput="True" EnableInlineProgress="True" MultipleFileSelection="Automatic" DropZones=".dropzone" />    
            <asp:Button runat="server" ID="Button1" Text="Submit" OnClick= "fileUpload" />

 

What would the code behind look like etc?

 

Thanks so much

4 Answers, 1 is accepted

Sort by
0
Neil
Top achievements
Rank 1
answered on 07 Jun 2017, 04:18 PM
Hi Phil,
It took me a while to sort this one out too. Hopefully the info below should put you in the right direction.

The way i got this to work was to the PostbackTriggers and the OnFileUploaded properties of the RadAsyncUpload control. 

In my example I have also set the TemporaryFolder to TempFold and applied the relevant write permissions to this folder (this is where the AsyncUpload control stores its files before uploading) if the permissions are not set correctly then the indicator will be red.

The PostbackTriggers property is the button click that you want to use to trigger the event for the upload process.
The OnFileUploaded property allows you to hook into the event so you control where you want to save the files.

<telerik:RadAsyncUpload  runat="server" ID="RadAsyncUpload1" MultipleFileSelection="Automatic" RenderMode="Mobile" DropZones="#DropZone1" OnFileUploaded="RadAsyncUpload1_FileUploaded" TemporaryFolder="TempFold"  HideFileInput="true" PostbackTriggers="btnSubmit" EnableInlineProgress="true"    />

<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click"  >SUBMIT</asp:LinkButton>

protected void btnSubmit_Click(Object Sender, EventArgs e)
{
        // Code to do something like making sure a file has been selected for upload
 // after the code in this event is complete the RadAsyncUpload1_FileUploaded will be called
}


 protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
 {
 
        // Loops through all files and saves to a specified folder path
            try
            {
                string SaveDir = "FolderPathHere";
          foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles)
                {
                    file.SaveAs(Server.MapPath(SaveDir)+@"\"+ FileName);
                }
            }
            catch (Exception ex)
            {
                string err = ex.Message;
            }
        }
 }
0
David
Top achievements
Rank 1
answered on 07 Jun 2017, 05:10 PM

Does something need to be written inside of the btnSubmit_Click event handler?

 

Because if not, I was unable to have success with this

0
Neil
Top achievements
Rank 1
answered on 07 Jun 2017, 09:54 PM

If you don't want to do anything before uploading files then you do not need the btnSubmit_Click event.
Providing your web.config file is correct and you have registered the correct handlers and modules then the below code should work just fine. If not then it would help if you can include your page code.


<telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" RenderMode="Mobile" OnFileUploaded="RadAsyncUpload1_FileUploaded" TemporaryFolder="tempFold" HideFileInput="true" PostbackTriggers="btnSubmit" EnableInlineProgress="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Upload Files" Width="100%" />
       
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        try
        {
            string SaveDir = "Files";
            if (!Directory.Exists(Server.MapPath(SaveDir)))
            {
                Directory.CreateDirectory(Server.MapPath(SaveDir));
            }
            foreach (UploadedFile file in RadAsyncUpload1.UploadedFiles)
            {
                file.SaveAs(Server.MapPath(SaveDir) + @"\" + file.FileName);
            }
        }
        catch (Exception ex)
        {
            string err = ex.Message;
        }
    }


0
David
Top achievements
Rank 1
answered on 08 Jun 2017, 01:35 PM

This is my web.config

 

 

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
  </connectionStrings>
  <appSettings>
    
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization" />
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" />
    </authentication>
    <httpHandlers>
      <add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false" />
    </httpHandlers>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" />
    </handlers>
  </system.webServer>
</configuration>

Tags
AsyncUpload
Asked by
David
Top achievements
Rank 1
Answers by
Neil
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or