Hi,
I am uploading two files to API of the Mobile Phone Service Company to create MMS Message such as one png and one audio. it's created MMS Message , but it's not working my audio file.Otherwise,I can upload 4 pieces image same those codes.Where I wrong?
Here is my code.
I am uploading two files to API of the Mobile Phone Service Company to create MMS Message such as one png and one audio. it's created MMS Message , but it's not working my audio file.Otherwise,I can upload 4 pieces image same those codes.Where I wrong?
Here is my code.
Code:
public byte[] UploadFiles(string address, IEnumerable<UploadFile>
files,NameValueCollection values)
{
var request = HttpWebRequest.Create(address);
request.Method = "POST";
var boundary = "---------------------------5646198165461";
sendInfoLog(request.Method, "", "");
sendInfoLog(boundary,"","");
sendInfoLog(request.ContentType, "", "");
boundary = "--" + boundary;
try
{
using (var requestStream = request.GetRequestStream())
{
// Write the values
foreach (string name in values.Keys)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
sendInfoLog(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}\n{2}",name, Environment.NewLine, values[name] + Environment.NewLine),"","");
}
// Write the files
int k = 0;
foreach (var file in files)
{
int c = 0;
foreach (var count in file.ContentType)
{
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name[c], file.Filename[c], Environment.NewLine));
sendInfoLog(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name[c], file.Filename[c], Environment.NewLine), "", "");
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType[c], Environment.NewLine));
sendInfoLog(string.Format("Content-Type: {0}{1}{1}", file.ContentType[c], Environment.NewLine), "", "");
requestStream.Write(buffer, 0, buffer.Length);
CopyTo(file.Stream, requestStream);
buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
++c;
}
++k;
}
var boundaryBuffer = Encoding.ASCII.GetBytes(boundary+"--\n");
requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
}
}
catch (Exception x)
{
sendErrorLog("Uploading Error","",x.Message,new Exception(""));
Console.Write("Error >" + x.Message);
}
using (var response = request.GetResponse())
using (var responseStream = response.GetResponseStream())
using (var stream = new MemoryStream())
{
CopyTo(responseStream, stream);
return stream.ToArray();
}
}
public static void CopyTo(Stream input, Stream output)
{
byte[] buffer = new byte[16 * 1024];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
public class UploadFile
{
public UploadFile(Stream stream)
{
Name = new List<string>();
ContentType = new List<string>();
Filename = new List<string>();
this.Stream = stream;
}
public readonly List<string> Name;
public readonly List<string> Filename;
public readonly List<string> ContentType;
public readonly Stream Stream;
}
public string CreateMMS(List<List<string>> Pathes,string name, string framecount, List<string> Frame_N_Text,List<string> Frame_N_Duration, List<List<string>> ContentType, List<List<string>> Names,string ProcessType)
{
//return ">>name"+name+"count="+" "+framecount+"text"+Frame_N_Text[0]+" "+Frame_N_Duration[0];
List<UploadFile> ufs = new List<UploadFile>();
int N=1;
try
{
for (int i = 0; i < Names.Count; ++i)
{
for (int j = 0; j < ContentType[i].Count; j++)
{
FileStream stream = File.Open(Pathes[i][j], FileMode.Open);
UploadFile uf = new UploadFile(stream);
uf.ContentType.Add(ContentType[i][j]);
uf.Name.Add(Names[i][j]);
uf.Filename.Add(Pathes[i][j]);
ufs.Add(uf);
}
}
NameValueCollection FrameValues = new NameValueCollection();
FrameValues.Add("token", Token);
FrameValues.Add("name", name);
FrameValues.Add("frame_count",framecount);
int loop = 0;
int forloop = Frame_N_Text.Count;
if (ProcessType == "4")
{
forloop=forloop/2;
}
for (int i = 0; i < forloop; i++)
{
FrameValues.Add("frame_" + N + "_text", Frame_N_Text[i]);
N++;
}
N = 1;
for (int i = 0; i < forloop; i++)
{
FrameValues.Add("frame_" + N + "_duration", Frame_N_Duration[i]);
N++;
}
byte[] RetMMSID = UploadFiles("https://api.xxx.xxx.com/mms.xml", ufs, FrameValues);
for (int i = 0; i < Names.Count; ++i)
{
ufs[i].Stream.Close();
}
string RetVal = System.Text.ASCIIEncoding.ASCII.GetString(RetMMSID);
return RetVal;
}
catch (Exception ex)
{
return "Uploading Error"+ex.Message;
}
}