Upload Attachments Before Saving an ASP.NET Web Forms Record

ASP.NET Web Forms · Published · AjaxUploader team

Uploading a file and saving a business record are two separate operations. Model that separation explicitly so a failed form submission does not lose the user's upload or attach it to the wrong record.

Separate receiving bytes from accepting an attachment

A support ticket, invoice or application form often has more validation than the file picker. Uploading early can spare the user from selecting a large file twice, but the upload should begin as a temporary object, not a published attachment. Record an opaque upload identifier, its owner, its expiry and a server-generated storage key. The original name is display metadata.

The completed upload callback tells the page which temporary object arrived. It does not prove that the current form is valid or that the user can attach the object to a particular ticket. Store identifiers on the page, then resolve them against trusted server metadata when the final Save button is submitted.

Validate the final form before linking files

The Save event should validate ordinary fields, authorize the destination record and load each pending upload under the current authenticated user or tenant. Reject an identifier that is expired, already consumed elsewhere or owned by someone else. A hidden field is useful for transporting the selection, but it is still client input.

// Application workflow, not an AjaxUploader event signature:
// 1. Validate the page and authorize the target record.
// 2. Load pending uploads owned by the current user.
// 3. Commit the record and attachment links in one transaction.
// 4. Mark those uploads committed; return the record identifier.

When files live outside the database, a database transaction cannot make a file move atomic. Use explicit states and a retryable promotion operation. If the database commit fails after a file copy, retain enough metadata to retry or clean it up. Avoid deleting the only copy before the record transaction succeeds.

Make double-clicks and retries safe

Users double-click Save, a connection drops after the server commits, and browsers resend requests. Treat the final operation as idempotent. One practical boundary is an attachment table with a uniqueness constraint on the record and upload identifiers, together with a request key for creating the record itself.

CREATE UNIQUE INDEX UX_RecordAttachment_Upload
ON dbo.RecordAttachment (RecordId, UploadId);

This SQL example prevents duplicate links for the same pair; it is not a substitute for authorization or a complete transaction design. Decide whether your business permits one upload to be linked to more than one record. Enforce that separately if it does not.

Show temporary and committed states honestly

Label an early upload as ready to attach, not saved to the ticket. After a form validation error, redisplay only the temporary uploads that are still valid for that user. If the user removes one, cancel the attachment intent and let the server apply the appropriate temporary-object lifecycle. Never accept a browser-supplied filesystem path.

  • An invalid form leaves the pending upload available for an appropriate retry window.
  • A second user cannot claim the first user's upload identifier.
  • A repeated Save returns the original result rather than creating duplicate attachments.
  • A background cleanup operation cannot delete an upload while a valid commit is promoting it.

Fit this design around AjaxUploader

Use AjaxUploader to handle transfer and file-selection UX, then connect its documented completion events to your pending-upload record. Keep business validation in your application. Start with the form integration demos and verify the installed version's event arguments before copying code from an older Web Forms example.

Common questions

Should I keep uploaded file bytes in ViewState?

No. Keep a server-side temporary object and transport only a short identifier. ViewState is not a file store.

Can I delete all temporary files when a user leaves the page?

No reliable browser event guarantees that cleanup. Use a server-side expiry policy that excludes committed and actively promoted objects.

Official references

Examples describe the stated framework and application workflow. Check your installed component version and hosting configuration before adapting them.

Evaluate AjaxUploader for ASP.NET Web Forms