silverlight 4 file upload to mvc 3 controller HttpPostedFileBase is null - asp.net-mvc-3

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 ActionResult 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);
}
}

Related

while streaming video file, file is getting locked by another process using PushStreamContent..how to solve it

I am trying to stream video file . when i open the same video file in another tab of browser , i get the message "file is being used by another process" . if I use FileShare.ReadWrite in file.open method then error goes away but video doesn't play in browser . can someone pl. help .
public HttpResponseMessage Get([string id)
{
var path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["path"] + "/" + id);
var video = new VideoStream(path);
HttpResponseMessage response = Request.CreateResponse();
var contentType = ConfigurationManager.AppSettings[Path.GetExtension(id)];
response.Content = new PushStreamContent(video.WriteToStream, new MediaTypeHeaderValue(contentType));
return response;
}
public class VideoStream
{
private readonly string _filename;
public VideoStream(string filename)
{
_filename = filename;
}
public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
{
try
{
var buffer = new byte[65536];
using (var video = File.Open(_filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
var length = (int) video.Length;
var bytesRead = 1;
while (length > 0 && bytesRead > 0)
{
bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
await outputStream.WriteAsync(buffer, 0, bytesRead);
length -= bytesRead;
video.Flush();
}
}
}
catch (HttpException ex)
{
return;
}
finally
{
// outputStream.Close();
// outputStream.Flush();
}
}
}
You should use:
File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
Assuming the file lock comes from the server. Is that the case, or is it a client side thing?

How to return result to UI thread in Windows Phone?

Does anyone know great idea How to return result to UI thread ?
I wrote this code, but It will be compile error because it can't return "img" in async.
public byte[] DownloadAsync2(Uri address)
{
byte[] img;
byte[] buffer = new byte[4096];
var wc = new WebClient();
wc.OpenReadCompleted += ((sender, e) =>
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = e.Result.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (e.Error == null) img = memoryStream.ToArray();
});
}
}
);
wc.OpenReadAsync(address);
return img; //error : Use of unassigned local variable 'img'
}
Change your method to:
public void DownloadAsync2(Uri address, Action<byte[]> callback, Action<Exception> exception)
{
var wc = new WebClient();
wc.OpenReadCompleted += ((sender, e) =>
{
using (MemoryStream memoryStream = new MemoryStream())
{
int count = 0;
do
{
count = e.Result.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (count != 0);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (e.Error == null) callback(memoryStream.ToArray());
else exception(e.Error);
});
}
}
);
wc.OpenReadAsync(address);
}
Usage:
DownloadAsync2(SomeUri, (img) =>
{
// this line will be executed when image is downloaded,
// img - returned byte array
},
(exception) =>
{
// handle exception here
});
Or (old-style code without lambda expressions):
DownloadAsync2(SomeUri, LoadCompleted, LoadFailed);
// And define two methods for handling completed and failed events
private void LoadCompleted(byte[] img)
{
// this line will be executed when image is downloaded,
// img - returned byte array
}
private void LoadFailed(Exception exception)
{
// handle exception here
}

Windows Phone 7 - Upload file to FTP server

Hy.
I do an application in WP7 which is connet a FTP server. I would like to upload a photo(with photochoosertask).
I wrote a PhotoChooserTask() which I could choose a photo. The program save the photo name(samplephoto01.jpg) and the photo route.
And I wrote a code which send command to FTP server:
public static void Execute(String msg)
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
Byte[] cmd = Encoding.UTF8.GetBytes((msg + "\r\n").ToCharArray());
socketEventArg.SetBuffer(cmd, 0, cmd.Length);
socket.SendAsync(socketEventArg);
}
This code i can chose the photo:
public void SelectAndUpLoad()
{
PhotoChooserTask p = new PhotoChooserTask();
p.Completed += new EventHandler<PhotoResult>(pt_Completed);
p.ShowCamera = true;
p.Show();
}
void pt_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ChosenPhoto);
MediaLibrary library = new MediaLibrary();
string PhotoPath = e.OriginalFileName;
// MessageBox.Show(PhotoPath);
for (int i = 0; i < library.Pictures.Count; i++)
{
Stream s = library.Pictures[i].GetImage();
if (s.Length == e.ChosenPhoto.Length)
{
string filename = library.Pictures[i].Name;
MessageBoxResult m = MessageBox.Show(filename, "Upload?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
Ftp.UploadFile(PhotoPath);
}
else
{
return;
}
break;
}
}
}
}
And this is the code whic i would like to upload the file:
public static void UploadFile(string file)
{
FileStream stream = new FileStream(file, FileMode.Open);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + file);
stream.Seek(0, SeekOrigin.Begin);
stream.Close();
}
But when i use the UploadFile(); method the program answer this:
MethodAccessException was unhandled
This code:
.
.
Ftp.UploadFile(PhotoPath);
}
else
{ //MethodAccessException
return;
}
break;
}
What was the wrong? Thank you!
I rewrote this code with IsolatedStorage to this:
for (int i = 0; i < library.Pictures.Count; i++)
{
Stream s = library.Pictures[i].GetImage();
if (s.Length == e.ChosenPhoto.Length)
{
string filename = library.Pictures[i].Name;
MessageBoxResult m = MessageBox.Show(filename, "Upload?", MessageBoxButton.OKCancel);
if (m == MessageBoxResult.OK)
{
IsolatedStorageFile iss = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fs = iss.OpenFile(PhotoPath, FileMode.Open);
Ftp.UploadFile(fs, filename);
fs.Close();
}
else
{
return;
}
break;
}
}
And the UploadFile() method:
public static void UploadFile(IsolatedStorageFileStream file, string RemoteFile)
{
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
int bytes;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + RemoteFile);
file.Seek(0, SeekOrigin.Begin);
while ((bytes = file.Read(buffer, 0, buffer.Length)) > 0)
{
socketEventArg.SetBuffer(buffer, bytes, 0);
socket.SendAsync(socketEventArg);
}
}
But i get an exception in this source:
IsolatedStorageFileStream fs = iss.OpenFile(PhotoPath, FileMode.Open);
The exception is: IsolatedStorageException was unhadnled.
What is wrong?
I think your problem lies in the line:
FileStream stream = new FileStream(file, FileMode.Open);
You can't open files this way on WP7. To get a stream to a file, you can either open it from the Isolated Storage (given that the file is stored there), or use the stream provided by a built-in method.
In your case, you have the stream with the property e.ChosenPhoto. Why don't you use it directly?
public static void UploadFile(Stream stream, string file)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Execute("STRO " + file);
stream.Seek(0, SeekOrigin.Begin);
stream.Close();
}
Then call UploadFile using e.ChosenPhoto as the first argument.

WP7 - POST form with an image

I need to send an image from the Windows Phone 7 to some e-mail addresses.
I use this class to submit text values to a PHP script, wich parses data and sends a formatted e-mail to the addresses.
The problem is that I can't figure out how to send an image to that script, to attach the image to the e-mail. The PHP script can be changed in any way. If I have an Image object, how can I change this class to allow sending images?
public class PostSubmitter
{
public string url { get; set; }
public Dictionary<string, string> parameters { get; set; }
public PostSubmitter() { }
public void Submit()
{
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);
// Prepare Parameters String
string parametersString = "";
foreach (KeyValuePair<string, string> parameter in parameters)
{
parametersString = parametersString + (parametersString != "" ? "&" : "") + string.Format("{0}={1}", parameter.Key, parameter.Value);
}
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(parametersString);
// Write to the request stream.
postStream.Write(byteArray, 0, parametersString.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
//Action<string> act = new Action<string>(DisplayResponse);
//this.Dispatcher.BeginInvoke(act, responseString);
}
I use the class in this way:
Dictionary<string, string> data = new Dictionary<string, string>()
{
{"nom", nom.Text},
{"cognoms", cognoms.Text},
{"email", email.Text},
{"telefon", telefon.Text}
};
PostSubmitter post = new PostSubmitter() { url = "http://example.com/parserscript.php", parameters = data };
post.Submit();
Thank you very much!
I've converted the above code to the following, I'm sure it will help:
public class PostSubmitter
{
public string url { get; set; }
public Dictionary<string, object> parameters { get; set; }
string boundary = "----------" + DateTime.Now.Ticks.ToString();
public PostSubmitter() { }
public void Submit()
{
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
myRequest.Method = "POST";
myRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
writeMultipartObject(postStream, parameters);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
}
public void writeMultipartObject(Stream stream, object data)
{
StreamWriter writer = new StreamWriter(stream);
if (data != null)
{
foreach (var entry in data as Dictionary<string, object>)
{
WriteEntry(writer, entry.Key, entry.Value);
}
}
writer.Write("--");
writer.Write(boundary);
writer.WriteLine("--");
writer.Flush();
}
private void WriteEntry(StreamWriter writer, string key, object value)
{
if (value != null)
{
writer.Write("--");
writer.WriteLine(boundary);
if (value is byte[])
{
byte[] ba = value as byte[];
writer.WriteLine(#"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentPhoto.jpg");
writer.WriteLine(#"Content-Type: application/octet-stream");
//writer.WriteLine(#"Content-Type: image / jpeg");
writer.WriteLine(#"Content-Length: " + ba.Length);
writer.WriteLine();
writer.Flush();
Stream output = writer.BaseStream;
output.Write(ba, 0, ba.Length);
output.Flush();
writer.WriteLine();
}
else
{
writer.WriteLine(#"Content-Disposition: form-data; name=""{0}""", key);
writer.WriteLine();
writer.WriteLine(value.ToString());
}
}
}
}
To convert an image from the camera to an byte array I've used the follwing:
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
try
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
foto.Source = image;
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(image);
// write an image into the stream
Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, image.PixelHeight, 0, 100);
byteArray = ms.ToArray();
}
}
catch (ArgumentNullException) { /* Nothing */ }
}
And I use the class this way:
Dictionary<string, object> data = new Dictionary<string, object>()
{
{"nom", nom.Text},
{"cognoms", cognoms.Text},
{"email", email.Text},
{"telefon", telefon.Text},
{"comentari", comentari.Text},
{"foto", byteArray},
};
PostSubmitter post = new PostSubmitter() { url = "http://example.com/parserscript.php", parameters = data};
post.Submit();
I don't know if it's the best way to send an image from the phone to a server, but I couldn't find anything, so I made my own class just reading this and that, and it has taken me several days. If anybody wants to improve the code or write any comment will be welcomed.
There are lots of questions/answers on here to help already
e.g.
Post with WebRequest - although i couldn't spot any specifically for photos.
Perhaps the best way is to use something like Hammock on Codeplex - http://hammock.codeplex.com/ - or perhaps something like RESTSharp - http://restsharp.org/ - they provide standard REST POST functions.
e.g. if you look within Hammock, then you'll find others who've posted images direct from the camera to tumblr - see http://hammock.codeplex.com/discussions/235650
The above code works perfect. I just use a different method to convert the file to an array of bytes which works perfect with Audio
public static class FileHelper
{
public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position;
stream.Position = 0;
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
stream.Position = originalPosition;
}
}
}

Is possible to create a new constructor that takes 3 arguments for FileStreamResult

Is it possible to create a constructor that takes 3 arguments for FileStreamResult. Taking into account from my code below, can you tell if I need to create private method. If so what code is required for that.
public FileResult GetImage(int id)
{
const string alternativePicturePath = #"/Content/question_mark.jpg";
MemoryStream stream;
MemoryStream streaml;
SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();
if ((z != null && z.Image1 != null) && (z != null && z.Image2 != null))
{
stream = new MemoryStream(z.Image1);
streaml = new MemoryStream(z.Image2);
}
else
{
var path = Server.MapPath(alternativePicturePath);
foreach (byte item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file.ContentLength == 0)
{
continue;
}
}
stream = new MemoryStream();
var imagex = new System.Drawing.Bitmap(path);
imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Seek(0, SeekOrigin.Begin);
streaml = new MemoryStream();
var imagey = new System.Drawing.Bitmap(path);
imagey.Save(streaml, System.Drawing.Imaging.ImageFormat.Jpeg);
streaml.Seek(0, SeekOrigin.Begin);
}
FileStreamResult newone = new FileStreamResult(); // not sure
//what additional code is needed here
return new FileStreamResult(stream, streaml,"image/jpg");
//'System.Web.Mvc.FileStreamResult' does not contain a
// constructor that takes 3 arguments
FileHandle for uploading iimages. I am using FileHandle for uploading images.
FileHandler.cs
public class FileHandler
{
public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
{
int nFileLen = file.ContentLength;
byte[] result = new byte[nFileLen];
file.InputStream.Read(result, 0, nFileLen);
return result;
}
}
To answer your direct question: No, you cannot create a different constructor for FileStreamResult because that class is part of the MVC framework (technically you could grab the source for MVC, make the changes, and compile it yourself but I would advise against this).
It would be better if you could provide more information about what you are trying to do. Do you want to concatenate the two streams? There's probably a different way to achieve your end goal.

Resources