将文本字段中的字符串附加到文件名

发布于 2024-12-15 18:41:30 字数 293 浏览 5 评论 0原文

我正在尝试将用户输入的字符串附加到文件名。

 [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data;   name=\"userfile\"; filename= \".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

我有一个文本字段,我希望将该字段中的输入附加到 .jpg 之前。我一直在玩我能找到的字符串东西,但不知道如何将它们放在上面。

I'm trying to append a string input from the user to a filename.

 [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data;   name=\"userfile\"; filename= \".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

I have a text field I would like the input from the field to be appended before the .jpg. I've been playing with the string stuff I can find, but don't know how to put them in up above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2024-12-22 18:41:30

我必须做出一些假设,因为你的问题不够清晰,但是......

假设 body 是一个 NSMutableData 对象。

假设您的 UITextField 名为“textFieldName”。

例如,假设 textFieldName 包含文本“TextInTextField”。

如果您希望生成一个字符串“Content-Disposition: form-data;”名称=“用户文件”; filename= "TextInTextField.jpg"',然后执行以下操作:

NSString *formattedString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename= \"%@.jpg\"\r\n",textFieldName.text];

stringWithFormat: & NSString 的 initWithFormat: 方法采用多个参数。第一个是格式或模板,用于将数据(来自以下参数)(在本例中为字符串)插入到所述格式中。您可以使用 %@%i 等指示插入数据的位置。在这种情况下,您将使用 %@ 插入字符串。

当然,这可以嵌套到原始的appendData:调用中,如下所示:

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename= \"%@.jpg\"\r\n",textFieldName.text] dataUsingEncoding:NSUTF8StringEncoding]];

I have to make some assumptions because your question lacks for clarity, but...

Assuming that body is an NSMutableData object.

Assuming that your UITextField is named 'textFieldName'.

Assuming, for example, that textFieldName contains the text 'TextInTextField'.

If you are looking to produce a string that reads 'Content-Disposition: form-data; name="userfile"; filename= "TextInTextField.jpg"', then do this:

NSString *formattedString = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename= \"%@.jpg\"\r\n",textFieldName.text];

The stringWithFormat: & initWithFormat: methods of NSString take multiple arguments. The first is a format, or template, that is used by inserting data (from the following arguments), in this case a string, into said format. You indicate where to insert the data with a %@ or %i and so on. In this case you would use %@ to insert a string.

Of course this can be nested into the original appendData: call like so:

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename= \"%@.jpg\"\r\n",textFieldName.text] dataUsingEncoding:NSUTF8StringEncoding]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文