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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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).

Enrico Rafols Dela Cruz user avatar by
Enrico Rafols Dela Cruz
·
Aug. 25, 18 · Tutorial
Like (2)
Save
Tweet
Share
11.19K 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.

Popular on DZone

  • How Agile Architecture Spikes Are Used in Shift-Left BDD
  • How To Handle Secrets in Docker
  • A First Look at Neon
  • Kubernetes-Native Development With Quarkus and Eclipse JKube

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: