开发者

Silverlight Upload file to MVC3 controller endpoint (Server Respose NotFound )

I'm developing a recorder in silverlight and I need to upload data from stream to the web server after recording process is completed. On server side I'm using ASP.NET MVC 3, and I hav开发者_如何学编程e created a Controller with method FileUpload.

public class FileUploaderController : Controller
{
    [HttpPost]
    public ActionResult FileUpload(string fileName)
    {
    ....
    }
}

In silverlight applet, the upload is made by parts, about 20000 bytes at time. Servers web config is configured to accept larger amount of data. Server returns an exception "The remote server returned an error: NotFound.".

In this case the request have not reached the action and I can't understand why.

Example of code that is used to start upload:

UriBuilder httpHandlerUrlBuilder = new UriBuilder("http://localhost:37386/FileUploader/FileUpload/?fileName=" + Guid.NewGuid() + ".wav");
   HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(httpHandlerUrlBuilder.Uri);
   webRequest.Method = "POST";
   webRequest.ContentType = "multipart/form-data"; // This solved my problem
   webRequest.BeginGetRequestStream(new AsyncCallback(WriteToStreamCallback), webRequest);

EDIT

My route configuration is by default:

 routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

When the small amount of data is sent, everything goes well and server receives the requested data. But when data to be send is larger I'm just getting NotFound response. This doesn't make any sense to me, what I'm doing is:

  • HttpWebRequest to send 20000 bytes
  • close request stream (obtained from request.EndGetRequestStream)
  • wait for server response (from webRequest.EndGetResponse) This is where error occurs.

In my case, I never send more than 20000 bytes, which is strange this to work sometimes and others not.

I don't know a better way to explain this problem. If you need I can provide more code and more information.

Any help is very much appreciated.


With filddler I was able to get more detailed information regarding to the error. It was "upload file potentially dangerous Request.Form value was detected from the client...".

To solve this I've specified content-type of the webRequest to "multipart/form-data"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜