gpt4 book ai didi

c# - 使用 asp.net 通过移动应用发送文件上传的步骤

转载 作者:行者123 更新时间:2023-11-29 12:09:10 24 4
gpt4 key购买 nike

我有移动应用程序,即基于 Android、IPhone 的应用程序,我必须在其中上传图像,在服务器端,我使用的是 asp.net、Web 服务“.asmx”。现在我不知道如何接收任何移动应用程序发送的文件,或者简单地说,我必须在服务器端做什么才能获取该文件并将其存储在我的本地系统中?我尝试了以下方法

  • 我已经创建了一个 asp.net 页面,我在其中放置了控件并使用 multipart/form-data 发布了文件,但是应用程序开发人员希望我为他们提供带有将接收文件名的参数的 url然后上传到服务器。

但是如何实现呢?

最佳答案

这就是我使用 WebMethod 实现文件上传的方式,android 和 Apple 应用程序都使用此 WebMethod 在服务器上上传文件,我使用 google chrome 扩展 Advanced rest client 对其进行了测试,以阅读有关 rest API Client 的更多信息 Click Here

[WebMethod]
public void UploadFile()
{
FileResponse master = new FileResponse();
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection Files = postedContext.Request.Files;
string sessionId = Server.UrlDecode((string)postedContext.Request.Form["sessionId"]);
master.sessionId = sessionId;
master.timeStamp = DateTime.Now.DdMonYyyy_HhMmSs();
master.status = Status.Failure;
master.sessionStatus = Status.Failure;
master.message = "Image not found";
try
{
if (isGuest_Employee(sessionId))
{
//Make sure a file was posted
string fileName = string.Format("feedbackImgSign_{0}.png", Helper.LDFileNumberSeq);
if (Files.Count == 1 && Files[0].ContentLength >= 1)
{
//The byte array we'll use to write the file with
byte[] binaryWriteArray = new
byte[Files[0].InputStream.Length];
//Read in the file from the InputStream
Files[0].InputStream.Read(binaryWriteArray, 0,
(int)Files[0].InputStream.Length);
//Open the file stream
FileStream objfilestream = new FileStream(Helper.UploadedFilesDirectory + "\\" +
fileName, FileMode.Create, FileAccess.ReadWrite);
//Write the file and close it
objfilestream.Write(binaryWriteArray, 0,
binaryWriteArray.Length);
objfilestream.Close();

master.FILE_NAME = fileName;
master.status = Status.Success;
master.sessionStatus = Status.Success;
master.message = "Image uploaded successfully!";
}
}
else
{
master.status = Status.Failure;
master.sessionStatus = Status.Failure;
master.message = "Unable to authenticate provided session Id";
}
}
catch (Exception ex)
{
master.status = Status.Failure;
master.message = ex.Message;
}

string strJSON = JsonConvert.SerializeObject(master, Formatting.Indented,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Write(strJSON);
}

关于c# - 使用 asp.net 通过移动应用发送文件上传的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33996330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com