上传文件到服务器不工作
本文关键字:工作 服务器 文件 | 更新日期: 2023-09-27 18:03:27
我这里有以下代码:
string imgPath = GetPathToImage(uri);
int imageHeight = 250;
int imageWidth = 200;
var bitmap = imgPath.LoadAndResizeBitmap2(imageWidth, imageHeight);
UploadBitmapAsync(bitmap);
async void UploadBitmapAsync(Bitmap bitmap)
{
const string UPLOAD_IMAGE = "http://" + HostAddress.Main + "/fsrservice/atxm";
byte[] bitmapData;
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
bitmapData = stream.ToArray();
var fileContent = new ByteArrayContent(bitmapData);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = "my_uploaded_image.jpg"
};
string boundary = "---8d0f01e6b3b5dafaaadaad";
MultipartFormDataContent multipartContent = new MultipartFormDataContent(boundary);
multipartContent.Add(fileContent);
HttpClientHandler clientHandler = new HttpClientHandler();
HttpClient httpClient = new HttpClient(clientHandler);
HttpResponseMessage response = await httpClient.PostAsync(UPLOAD_IMAGE, multipartContent);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("SUCCESS! " + content);
bitmap.Recycle();
}
else
{
Console.WriteLine("NOT SUCCESSFUL!");
}
}
它没有给出任何错误(给出NOT SUCCESSFUL
),但图像不会上传到服务器。我使用Xamarin Android为此,我基于我的代码在这里:在Xamarin Android中使用HTTP Multipart上传位图图像。fsrservice
是我为我的应用程序使用的web服务,这就是我把文件夹atxm
保存文件的地方。有什么问题吗?
好的,所以我仍然试图弄清楚,然后我偶然发现了这个方法,WebClient。UploadData方法。我将其添加到我的web服务并创建了.aspx,但我无法看到上传的文件到文件夹。从客户端,文件通过字节数组传递。
以下是我的代码:
Service1.svc.cs
public string FileUpload(EmployeeDetails userInfo)
{
Database context = new Database();
List<byte[]> byteArray = JsonConvert.DeserializeObject<List<byte[]>>(userInfo.File);
List<string> fileName = JsonConvert.DeserializeObject<List<string>>(userInfo.FileName);
List<string> editedName = new List<string>();
Attachment obj = new Attachment();
for (int i = 1; i <= byteArray.Count; i++)
{
for (int j = 0; j < fileName.Count; j++)
{
// SOME CODE HERE
}
}
context.SubmitChanges();
for (int i = 0; i < byteArray.Count; i++)
{
byte[] b = byteArray[i];
string uriString = "http://" + HostAddress.Address + "/FSRService/upload.aspx";
WebClient myWebClient = new WebClient();
myWebClient.UploadData(uriString, "POST", b);
}
string msg = null;
return msg;
}
upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upload.aspx.cs" Inherits="WcfService.upload" %>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {
foreach(string f in Request.Files.AllKeys) {
HttpPostedFile file = Request.Files[f];
file.SaveAs("c:''inetpub''wwwroot''FSRService''ATXM''" + file.FileName);
}
}
</Script>
<html>
<body>
<p> Upload complete. </p>
</body>
</html>
问题:
- 我如何编辑/添加我要上传的文件的文件名?
- 为什么文件没有上传到文件夹?
好的我所做的是这样的。
首先,我为文件使用一个像这样的对象:
class FileTransfer
{
public byte[] content;
public string name;
public FileTransfer(string name, byte[] content)
{
this.name= name;
this.content= content;
}
}
然后我有一个web服务在Asp。谁长得像:
[WebMethod]
public string GetFile(string data)//the file comes serialized to Json
{
//Transforms the string into the object
FileTransfer recived = new JavaScriptSerializer().Deserialize<FileTransfer>(data);
string path = "c:''inetpub''wwwroot''FSRService''ATXM''"+recived.name; //you could also do this in the object builder
File.WriteAllBytes(path, recived.content);
}
现在在Xamarin中,你可以直接添加你的。asmx webservice作为一个web引用,右键单击你的项目,添加,添加web引用,它将创建一个"类"与异步函数。
你可以这样使用:
public void SendFile(File fileToSend)
{
//process the file
FileStream objfilestream = new FileStream(fileToSend.FullName, FileMode.Open, FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents, 0, len);
objfilestream.Close();
FileTransfer newFiletoSend = new FileTransfer(file.Name, documentcontents);
string raw = new JavaScriptSerializer().Serialize(newFiletoSend);
//send it
nameOfTheWebServiceClass.nameOfTheAsmxFile service = new nameOfTheWebServiceClass.nameOfTheAsmxFile();
service.GetFile(raw); //GetFile is the name of the funcion you want to use in the .asmx webservice.
}