Hi ,
I am working in a web application, where, while uploading the files, I need to check that the files which is getting uploaded is corrupted or not using c#.
Hi ,
I am working in a web application, where, while uploading the files, I need to check that the files which is getting uploaded is corrupted or not using c#.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak RawatRank #646 · 3.7K reputationPosted Jul 25, 2023, 1:30 AM EDT
if a file is corrupted or not during the file upload process in a web application using C#, you can perform a simple integrity check based on the file's content or checksum. One common approach is to calculate and compare the checksum of the uploaded file with a pre-calculated checksum value.
Here's a step-by-step guide on how you can achieve this:
Calculate the checksum of the original file: Before the file is uploaded, calculate the checksum of the original file on the client-side or server-side. You can use cryptographic hash functions like MD5, SHA-1, or SHA-256 to calculate the checksum. For this example, I'll use SHA-256.
Send the checksum along with the file during upload: Include the checksum value as part of the file upload process. You can send it as a hidden form field, as part of a JSON object, or in any other suitable way.
Handle the file upload on the server-side: In your C# server-side code, receive the uploaded file and its associated checksum value.
Calculate the checksum of the uploaded file: Use the same cryptographic hash function (SHA-256) to calculate the checksum of the uploaded file on the server-side.
Compare the checksum values: Compare the calculated checksum of the uploaded file with the original checksum value. If they match, the file is likely not corrupted. Otherwise, it indicates potential corruption or data modification during transmission.
Here's a basic code outline for the server-side C#
using System; using System.IO; using System.Security.Cryptography; using System.Web; public partial class UploadFile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Files.Count > 0) { HttpPostedFile uploadedFile = Request.Files[0]; string originalChecksum = Request.Form["originalChecksum"]; // Get the original checksum value // Calculate the checksum of the uploaded file string uploadedFileChecksum = CalculateFileChecksum(uploadedFile.InputStream); // Compare the checksum values if (uploadedFileChecksum == originalChecksum) { // File is not corrupted // Proceed with file processing or saving } else { // File may be corrupted or modified // Handle the case where the file integrity is compromised } } } // Method to calculate the SHA-256 checksum of a stream private string CalculateFileChecksum(Stream stream) { using (SHA256 sha256 = SHA256.Create()) { byte[] checksumBytes = sha256.ComputeHash(stream); return BitConverter.ToString(checksumBytes).Replace("-", "").ToLower(); } } }AmitRank #9,090 · 211 reputationPosted Jul 25, 2023, 5:21 AM EDT
Thank you ...