DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Protect Node.js Form Uploads With a Deterministic Threat Detection API
  • Build a Time-Tracking App With ClickUp API Integration Using Openkoda
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Upload/Download a File To and From the Server

Trending

  • How to Convert XLS to XLSX in Java
  • Unlocking AI Coding Assistants: Generate Unit Tests
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Measuring the Impact of AI on Software Engineering Productivity
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Mule ESB 3.9: How to Upload a File via Web Form and Download File

Mule ESB 3.9: How to Upload a File via Web Form and Download File

This article will explain how to implement a File Upload via Web Form and Download File as the way of response from the HTTP POST request(Web Form).

By 
Enrico Rafols Dela Cruz user avatar
Enrico Rafols Dela Cruz
·
Updated Aug. 25, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

This article will explain how to implement a File Upload via Web Form and Download File as the way of response from the HTTP POST request(Web Form).

Image title

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.9.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<flow name="pf-file-upload-download-service" processingStrategy="synchronous">
        <logger message="#[message.rootId] - Start of the Service" level="INFO" doc:name="Logger"/>
                <message-properties-transformer scope="invocation" doc:name="Message Properties Get HTML Form Data">
            <add-message-property key="fileContent" value="#[message.inboundAttachments.file.dataSource.part.inputStream]"/>
            <add-message-property key="filename" value="#[message.inboundAttachments.file.dataSource.part.fileName]"/>
        </message-properties-transformer>
        <dw:transform-message doc:name="Transform Message Edit File Content">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
"EDITED BY MULE: \n" ++ flowVars.fileContent]]></dw:set-payload>
        </dw:transform-message>
        <logger message="#[&quot;\nFILENAME: &quot;+flowVars.filename+&quot;\nFILE CONTENT:\n&quot;+flowVars.fileContent]" level="INFO" doc:name="Logger File Details"/>
        <set-property propertyName="Content-Disposition" value="#['attachment; filename=EDITED_BY_MULE_'+flowVars.filename]" doc:name="Property Set Content-Disposition"/>
        <logger message="#[message.rootId] - End of the Service" level="INFO" doc:name="Logger"/>
    </flow>
    <sub-flow name="sf-response-web-page">
        <parse-template location="\web\index.html" doc:name="Parse Template"/>
        <logger message="#[message.rootId] - Response Web Page UI." level="INFO" doc:name="Logger"/>
    </sub-flow>
</mule>

pf-file-upload-download-service

MESSAGE-PROPERTIES (Get HTML Form Data): This will get the data from the HTML Form and assign to the Variable. Also Note that the #[message.inboundAttachments.file.dataSource.part.inputStream] is the name attribute from the HTML <input type="file" required="required" class="input-block-level" name="file"> .

<message-properties-transformer scope="invocation" doc:name="Message Properties Get HTML Form Data">
  <add-message-property key="fileContent" 
  value="#[message.inboundAttachments.file.dataSource.part.inputStream]"/>
  <add-message-property key="filename" 
  value="#[message.inboundAttachments.file.dataSource.part.fileName]"/>
</message-properties-transformer>

Transform Message (Edit File Content): This will just add a string EDITED BY MULE in the uploaded file content and make it as the payload.

<dw:transform-message doc:name="Transform Message Edit File Content">
  <dw:set-payload>
        <![CDATA[%dw 1.0
     %output application/java
     ---
     "EDITED BY MULE: \n" ++ flowVars.fileContent
]]>
  </dw:set-payload>
</dw:transform-message>

SET-PROPERTY (Set Content-Disposition): This indicates that the payload is an attachment(file) that can be downloaded and saved locally.

<set-property propertyName="Content-Disposition" 
              value="#['attachment; filename=EDITED_BY_MULE_'+flowVars.filename]" 
              doc:name="Property Set Content-Disposition"/>

sf-response-web-page

This will set the payload as HTML (Web Form) using parse-template. The actual HTML content is located in the resources\web\index.html

<sub-flow name="sf-response-web-page">
   <parse-template location="\web\index.html" doc:name="Parse Template"/>
   <logger message="#[message.rootId] - Response Web Page UI." level="INFO" doc:name="Logger"/>
</sub-flow>

RAML: 

GET: /file — Resource that will render the Web Form.

POST: /file — Resource that handles the Web Form Submission.

#%RAML 0.8
title: FILE UPLOAD

/file:
    get:
      responses:
        200:
          body:
            text/html:
    post: 
      body: 
        multipart/form-data:
          formParameters:
            file:
              required: true
      responses:
         200:
           body:
             multipart/form-data:

RAML GENERATED FLOW: Reference the corresponding flow implementation in the generated RAML Private Flows get:/file:file-config and post:/file:file-config

Image title

HTML FORM: HTML Form with method="POST" and action = "/api/file". This will redirect the user to the POST: /file resource flow after clicking the Submit button. Also, note that the enctype="multiple/form-data" should always be included in the HTML form attribute so that Mule will be aware what type of the POST request. (Which is a form-based data) and will able to handle it.

<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>File Upload</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
  </head>
  <body>
    <div class="container">
      <form class="form-signin" method="POST" action="/api/file" enctype="multipart/form-data">
        <h3 class="form-signin-heading">File Upload</h3>
        <input type="file" required="required" class="input-block-level" name="file">
        <button class="btn btn-large btn-primary" type="submit">Submit</button>
      </form>
    </div> <!-- /container -->
</body></html>

Test Result:

Input File Content:

Image title

Upload the SALARY_DATA_2018.txt via Web Form.

Image title

After the form submission, Mule responses an edited file with filename: EDITED_BY_MULE_SALARY_DATA_2018.txt

Image title

The downloaded file contains EDITED BY MULE followed by the original uploaded file content.
Image title

Mule logs show that the process was completed and Mule was able to get the data from the HTML Form.

Image title


Form (document) Enterprise service bus Upload Download HTML

Opinions expressed by DZone contributors are their own.

Related

  • How To Protect Node.js Form Uploads With a Deterministic Threat Detection API
  • Build a Time-Tracking App With ClickUp API Integration Using Openkoda
  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Upload/Download a File To and From the Server

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!