Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Ajax

Ajax: Fix a Multipart Boundary Breaking on a Long File Name

- 02.12.26 - ErcanOPAK

🔄 Ajax: Fix a Multipart Boundary Breaking on a Long File Name

When you build a multipart/form-data request body manually instead of letting the browser’s native FormData object handle it, every file part needs its filename field properly quoted and escaped inside the Content-Disposition header — and a file name containing an unescaped double quote, or one long enough combined with other metadata to push a line past what some older multipart parsers on the server side tolerate, can corrupt the boundary detection for that part, causing the server’s multipart parser to either truncate the file name or, in the worst case, misread where that part ends and the next one begins.

🔎 The Problem

Hand-built multipart body for a file named: My "Vacation" Photo (Final).jpg

------WebKitFormBoundary7MA4YWxk
Content-Disposition: form-data; name="file"; filename="My "Vacation" Photo (Final).jpg"
Content-Type: image/jpeg

<binary data>
------WebKitFormBoundary7MA4YWxk--

  ^ the unescaped quotes inside the filename itself close the
    filename="..." attribute early - a strict server-side multipart
    parser reads "My " as the entire filename and then chokes on the
    unexpected 'Vacation" Photo (Final).jpg"' text that follows

✅ Fix: Let the Browser’s FormData Build the Body, Don’t Hand-Roll It

  • Use the native FormData API (const fd = new FormData(); fd.append('file', fileObject)) and let the browser serialize the multipart body and set the Content-Type boundary header itself — it already correctly percent-encodes or escapes problem characters in the filename per the spec, which a hand-built string template almost never gets fully right.
  • If the request genuinely has to be built manually (a non-browser environment, a specific legacy server quirk), escape at minimum double quotes and backslashes in the filename per RFC 7578 (\" and \\) before inserting it into the Content-Disposition header.
  • Consider renaming or sanitizing exotic filenames client-side before upload when the receiving server is known to use an older or stricter multipart parser, since even a technically spec-correct escaped filename can still trip parsers that were never tested against anything beyond simple ASCII names.

🚨 Why This Is Dangerous

  • A broken multipart boundary doesn’t always fail the whole request outright — some lenient parsers will silently misattribute bytes between parts, meaning a corrupted upload can succeed with a 200 response while quietly storing the wrong file name, a truncated file, or data bleeding from one form field into another.

Multipart boundaries are just string matching under the hood — anything in a filename that can look like the boundary’s own syntax is something you have to escape yourself the moment you stop letting the browser build the request for you.

— Frontend Engineer

Related posts:

Ajax Revolution: How Fetch API and Async/Await Replace jQuery.ajax()

Ajax Revolution: How Fetch API and Async/Await Replace jQuery.ajax()

Ajax: Use Server-Sent Events for Real-Time Feeds

Ajax: Use Server-Sent Events for Real-Time Feeds

Ajax: Fix a Request That Overwrites Newer Data With an Older Response That Arrived Late

Ajax: Fix a Request That Overwrites Newer Data With an Older Response That Arrived Late

Views: 0
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Kubernetes: Fix a VPA Evicting Pods More Than It Should
Windows 11: Fix the Print Spooler Jamming on One Stuck Job

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com