开发者

silverlight 4 file upload to mvc 3 controller HttpPostedFileBase is null

I have a mvc 3 page that I want to be able to upload images to my website using silverlight to do the uploading and present a progress bar and a cancel button as it uploads. But I keep getting null value in my controller for the HttpPostedFileBase argument.

Here is my silverlight upload code ...

        var client = new WebClient();
        client.Headers[HttpRequestHeader.ContentType] = "multipart/form-data";  
        client.OpenWriteCompleted += (sender1, e1) =>
        {                       
            PushData(stream, e1.Result);
            e1.Result.Close();
            stream.Close();
        };

        client.UploadProgressChanged += (sender1, e1) =>
        {
            this.pbStatus.Value = e1.ProgressPercentage;
        };

        // get uri from params
        param = App.Current.Host.InitParams["url"];
        var uri = new Uri(param, UriKind.Relative);
        client.OpenWriteAsync(uri, "POST");

Push Data method ...

    private void PushData(Stream input, Stream output)
    {
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }                                                         

And my controller code ...

[HttpPost]
public A开发者_Python百科ctionResult UploadTexture(HttpPostedFileBase file)
{ 
}

The file param in my controller is null when the controller is called. Anyone know what I am doing wrong ?

I've seen examples that implement a IHttpHandler but I'm trying to avoid doing that and stick with just straight mvc 3 controllers.


I was having the same issue that you were. I was able to solve this issue another way.

foreach (FileInfo fi in uploadedFiles)
        {
            UriBuilder ub = new UriBuilder(Application.Current.Host.Source.Host + "/Excel/?fileName=" + fi.Name);
            WebClient wc = new WebClient();
            wc.Headers[HttpRequestHeader.ContentType] = "multipart/form-data"; 
            wc.OpenWriteCompleted += (sender, e) =>
            {
                FileStream data = fi.OpenRead();
                PushData(data, e.Result);
                e.Result.Close();
                data.Close();
            };
            wc.OpenWriteAsync(ub.Uri, "POST");
        }

Main difference you will see is that I attach the filename to the URL. My PushData() is the same. On the MVC side, I have:

[HttpPost]
    public ActionResult Index(string fileName)
    {
        using (FileStream fs = System.IO.File.Create(Server.MapPath("~/FilesExcel/" + fileName)))
        {
            SaveFile(Request.InputStream, fs);
        }

        return View();
    }

    private void SaveFile(Stream stream, FileStream fs)
    {
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
        {
            fs.Write(buffer, 0, bytesRead);
        }
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜