C# ASP.NET WebAPI更改文件名?
我正在尝试将图像的文件名更改为我在输入框中发布的值C# ASP.NET WebAPI更改文件名?,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我正在尝试将图像的文件名更改为我在输入框中发布的值username。文件将上载到服务器,并且在覆盖GetLocalFileName后,文件名将从“BodyPart_uuz(xyz)”更改为原始文件名。如何将它们重命名为输入框中提供的值 <form name="form1" method="post" enctype="multipart/form-data" action="api/poster/postformdata"> <div class="ro
username
。文件将上载到服务器,并且在覆盖GetLocalFileName
后,文件名将从“BodyPart_uuz(xyz)”更改为原始文件名。如何将它们重命名为输入框中提供的值
<form name="form1" method="post" enctype="multipart/form-data" action="api/poster/postformdata">
<div class="row-fluid fileform">
<div class="span3"><strong>Username:</strong></div>
<input name="username" value="test" type="text" readonly/>
</div>
<div class="row-fluid fileform">
<div class="span3"><strong>Poster:</strong></div>
<div class="span4"><input name="posterFileName" ng-model="posterFileName" type="file" /></div>
</div>
<div class="row-fluid fileform">
<div class="span8"><input type="submit" value="Submit" class="btn btn-small btn-primary submitform" /></div>
</div>
</form>
一种方法是重写
ExecutePostProcessingAsync
方法,如下所示:
public override async Task ExecutePostProcessingAsync()
{
await base.ExecutePostProcessingAsync();
// By this time the file would have been uploaded to the location you provided
// and also the dictionaries like FormData and FileData would be populated with information
// that you can use like below
string targetFileName = FormData["username"];
// get the uploaded file's name
string currentFileName = FileData[0].LocalFileName;
//TODO: rename the file
}
谢谢。我不知道如何用新名称复制文件并删除旧文件。如何在
ExecutePostProcessingAsync()
本身中实现这一点?
public override async Task ExecutePostProcessingAsync()
{
await base.ExecutePostProcessingAsync();
// By this time the file would have been uploaded to the location you provided
// and also the dictionaries like FormData and FileData would be populated with information
// that you can use like below
string targetFileName = FormData["username"];
// get the uploaded file's name
string currentFileName = FileData[0].LocalFileName;
//TODO: rename the file
}