Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET WebAPI更改文件名?_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

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
}