Bạn có thể kế thừa từ MultipartFormDataStreamProvider và ghi đè GetLocalFileName (chạy sau khi đọc nội dung vào luồng) hoặc GetStream (chạy trước khi đọc nội dung vào luồng). Trong cả hai trường hợp, bạn có quyền truy cập vào headers.ContentDisposition.FileName
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path)
: base(path)
{
}
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
//validate headers.ContentDisposition.FileName as it will have the name+extension
//then do something (throw error, continue with base or implement own logic)
}
public override Stream GetStream(HttpContent parent, System.Net.Http.Headers.HttpContentHeaders headers)
{
//validate headers.ContentDisposition.FileName as it will have the name+extension
//then do something (throw error, continue with base or implement own logic)
}
}
Nguồn
2012-09-04 09:09:55