我想使用" PUT"在Windows Phone 8中使用后台传输请求从webdav服务器中的照片选择器任务上传文件。 Method.Problem是文件出现在服务器中并显示为0kb。如果有人知道请发布如何使用后台传输请求发送文件流作为带有后台传输请求的字节。如果有人知道帮助我......
这是我的代码:
private void Upload_Click(object sender, EventArgs e)
{
progress.IsIndeterminate = true;
progress.IsVisible = true;
var photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += PhotoChooserTaskCompleted;
photoChooserTask.Show();
}
private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
BinaryReader objReader = new BinaryReader(e.ChosenPhoto);
filename = Path.GetFileName(e.OriginalFileName);
MessageBox.Show("filename...." + filename);
//MessageBox.Show(filename);
sbytedata = ReadToEnd(e.ChosenPhoto);
var parameters = new Dictionary<string, object>();
parameters.Add("photo", sbytedata);
string url = String.Format("{0:g}://{1:g}", _workingAccount.Protocol, _workingAccount.ServerDomain + _workingPath + filename);
UploadInBackground(url, parameters, _workingAccount,e.ChosenPhoto);
}
private void UploadInBackground(string url, Dictionary<string, object> parameters, Account _workingAccount, Stream stream)
{
// Check if there're already 5 requests.
if (BackgroundTransferService.Requests.Count() >= 1)
{
MessageBox.Show("Please wait until other records have been uploaded.");
return;
}
// Store the file in isolated storage.
var iso = IsolatedStorageFile.GetUserStoreForApplication();
if (!iso.DirectoryExists("/shared/transfers"))
{
iso.CreateDirectory("/shared/transfers");
}
using (var fileStream = iso.CreateFile("/shared/transfers/"+filename))
{
stream.CopyTo(fileStream);
}
// Transfer the file.
try
{
BackgroundTransferRequest request = new BackgroundTransferRequest(new Uri(url));
request.Method = "PUT";
var credentials = new UTF8Encoding().GetBytes(_workingAccount.Username + ":" + _workingAccount.Password);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentials);
request.Headers["ContentType"] = string.Format("multipart/form-data; boundary={0}", this._boundary);
request.UploadLocation = new Uri("shared/transfers/"+filename, UriKind.Relative);
request.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
request.Tag = request.UploadLocation.ToString();
OnBackgroundTransferStatusChanged(request);
BackgroundTransferService.Add(request);
}
catch
{
MessageBox.Show("Unable to upload the file at the moment. Please try again later.");
}
}
private void OnBackgroundTransferStatusChanged(BackgroundTransferRequest request)
{
if (request.TransferStatus == TransferStatus.Completed)
{
BackgroundTransferService.Remove(request);
if (request.StatusCode == 201)
{
MessageBox.Show("Upload completed.");
}
else
{
MessageBox.Show("An error occured during uploading. Please try again later.");
}
}
}
答案 0 :(得分:0)
我想这里的问题是,stream.CopyTo(fileStream);
没有将数据保存在上传位置。您可以使用Windows Phone电源工具验证这一点,并检查文件是否已创建且数据是否已在指定的上载位置成功复制。