2

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());

    }
1
  • I edited the question above to show another file rename test.
    – forwheeler
    Dec 4, 2013 at 3:17

1 Answer 1

2

eBy trial and error, I added a call to update status:

void Handler_StoreFileRequestStarted(object sender, Backload.Eventing.Args.StoreFileRequestEventArgs e)
{
    var file = e.Param.FileStatusItem;
    file.FileName = "test.jpg";       // Or whatever logic needed to change the file.
    file.UpdateStatus(true);
}

The 'true' is a parameter named updateStorageInfo which was needed to make it stick.

2
  • 2
    Thanks, I feel like this should have been clearly documented in their example write-up. FYI to anyone else, the code snippet above is done in the handler_StoreFileRequestStartedAsync handler Jul 25, 2014 at 15:30
  • Good point. I updated the answer to include the whole method.
    – goodeye
    Jul 26, 2014 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.