I would like to rename the file before the file is uploaded. I am able to rename it in the JavaScript so it displays in the view but I'm not sure how to rename it on the server so it can be saved in the file system with the new name. I see some examples using PHP but not for MVC controller in .Net. The sample controller only has an Action to return the view so I'm not sure how to intercept the POST. Does anyone have some working code to do this?
UPDATE I used the code at https://github.com/blackcity/Backload/tree/master/Examples/Example04 this way to try to rename the file without success.
public class FileUploadInstanceController : Controller
{
public async Task<ActionResult> FileHandler()
{
FileUploadHandler handler = new FileUploadHandler(Request, this);
handler.IncomingRequestStarted += handler_IncomingRequestStarted;
ActionResult result = await handler.HandleRequestAsync();
return result;
}
void handler_IncomingRequestStarted(object sender, Backload.Eventing.Args.IncomingRequestEventArgs e)
{
// Demo: Disallow PUT request within the event handler.
if (e.Context.HttpMethod == "PUT") e.Context.PipelineControl.ExecutePipeline = false;
var values = e.Param.BackloadValues;//added
values.FileName = "test.jpg";//added
}
}
UPDATE 2 I think I need to change the file name in this handler but it doesn't work either. https://github.com/blackcity/Backload/wiki/Example-12
async Task handler_StoreFileRequestStartedAsync(object sender, Backload.Eventing.Args.StoreFileRequestEventArgs e)
{
// Demo 6: Change the file to be stored
string filepath = Server.MapPath("~/Files/test.jpg");
var file = e.Param.FileStatusItem;
file.FileName = "test.jpg";//added
//if (System.IO.File.Exists(filepath))
//{
// byte[] bytes = null;
// using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
// {
// bytes = new byte[stream.Length];
// await stream.ReadAsync(bytes, 0, (int)stream.Length);
// }
// file.FileData = bytes;
// file.FileSize = bytes.LongLength;
// file.ContentType = "image/jpeg";
//}
e.Context.PipelineControl.Message.MessageText += string.Format(_logpattern, "log-post", "StoreFileRequestStartedAsync", DateTime.Now.ToLongTimeString());
}