Hey Stackoverflow'
我使用ASP.net WebApi从Cordova应用程序接收上传(通过HTTP多部分POST请求发送)。上传是" chunked"因此,我需要在第一个块上创建一个文件,然后追加每个后续的" chunk"它。以下是我目前正在使用的内容:
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path) : base(path)
{ }
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
return name.Replace("\"", string.Empty); //this is here because Chrome submits files in quotation marks which get treated as part of the filename and get escaped
}
}
并在控制器......
[Route("upload"), HttpGet, HttpPost]
public void HandleUpload()
{
string root = HttpContext.Current.Server.MapPath("~/App_Data/upload-temp");
if (Request.Content.IsMimeMultipartContent())
{
var streamProvider = new Libs.CustomMultipartFormDataStreamProvider(root);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw new HttpResponseException(HttpStatusCode.InternalServerError);
});
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}
我在CustomMultipartFormDataStreamProvider中假设我会以某种方式覆盖GetStream以完成追加,但是对于我的生活我不能理解它...
有没有人对如何做到这一点有所了解?
干杯:)
答案 0 :(得分:1)
这篇文章正是我正在处理的问题,其诀窍是覆盖自定义 MultipartFormDataStreamProvider 中的 GetStream 方法。我的解决方案基于System.Net.Http.MultipartFormDataStreamProvider提供程序的源代码。
void tileInit()
{
struct tileStruct * tileArr = (struct tileStruct *)malloc(NUM_TILES_IN_WINDOW * sizeof(struct tileStruct));
tileArr->tilePixel = (SDL_Rect *) malloc(tPIXELS_PER_TILE * sizeof(SDL_Rect));
tileArr->tpColor = (Uint32 *) malloc(tPIXELS_PER_TILE * sizeof(Uint32));
tileArr->xCoord = (int *) malloc(sizeof(int));
tileArr->yCoord = (int *) malloc(sizeof(int));
tileArr->tileNum = (int *) malloc(sizeof(int));
//rest of function
}