【发布时间】:2014-01-01 00:17:43
【问题描述】:
我使用下面的代码来接收上传的文件,并将它们存储在服务器上的uploadedFiles文件夹中。
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<HttpResponseMessage> 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<string> files = new List<string>();
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);
}
}
现在我想重命名上传的文件。据我了解,存储和重命名必须在关键部分完成,因为当另一个用户同时上传同名文件时,第一个将被覆盖。
这就是我想象的解决方案,但是锁定语句中不允许使用await。
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
}
}
我如何保证文件将被自动存储和重命名?
【问题讨论】:
-
为什么不将文件创建为元组
第一个字符串是原始文件名(file.LocalFileName),第二个字符串是所需(重命名)文件名
标签: c# asp.net asp.net-mvc file-upload multipartform-data