Note that there are some explanatory texts on larger screens.

plurals
  1. PORenaming concurrent uploaded files in ASP.NET
    primarykey
    data
    text
    <p>I use the following code to receive uploaded files and store them in the uploadedFiles folder on the server.</p> <pre><code>public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider { public CustomMultipartFormDataStreamProvider(string path) : base(path) { } public override string GetLocalFileName(HttpContentHeaders headers) { return headers.ContentDisposition.FileName.Replace("\"", string.Empty); } } [HttpPost] public async Task&lt;HttpResponseMessage&gt; ReceiveFileupload() { if (!Request.Content.IsMimeMultipartContent("form-data")) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); // Prepare CustomMultipartFormDataStreamProver in which our multipart form data will be loaded string fileSaveLocation = HttpContext.Current.Server.MapPath("~/UploadedFiles"); CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation); List&lt;string&gt; files = new List&lt;string&gt;(); Duck duck = null; try { // Read all contents of multipart message into CustomMultipartFormDataStreamProvider await Request.Content.ReadAsMultipartAsync(provider); // Parse an ID which is passed in the form data long id; if (!long.TryParse(provider.FormData["id"], out id)) { return Request.CreateResponse(HttpStatusCode.ExpectationFailed, "Couldn't determine id."); } duck = db.Ducks.Find(id); if (null == duck) return Request.CreateResponse(HttpStatusCode.NotAcceptable, "Duck with ID " + id + " could not be found."); // Loop through uploaded files foreach (MultipartFileData file in provider.FileData) { // File ending needs to be xml DoSomething(); // On success, add uploaded file to list which is returned to the client files.Add(file.LocalFileName); } // Send OK Response along with saved file names to the client. return Request.CreateResponse(HttpStatusCode.OK, files); } catch (System.Exception e) { return Request.CreateResponse(HttpStatusCode.InternalServerError, e); } } </code></pre> <p>Now I want to rename the uploaded files. As I understand, storing and renaming must be done in a critical section because when another user uploads a file with the same name at the same time, the first will be overwritten.</p> <p>That's how I imagine a solution, but <code>await</code> is not allowed in a lock statement.</p> <pre><code>lock(typeof(UploadController)) { await Request.Content.ReadAsMultipartAsync(provider); //That's the line after which the uploaded files are stored in the folder. foreach (MultipartFileData file in provider.FileData) { RenameFile(file); // Rename according to file's contents } } </code></pre> <p>How can I guarantee that the files will be stored and renamed atomically?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload